Cannot resolve symbol 'x' - java

Hello I'm starting in Android Studio and I wanted to know how to pass from one activity to another. I've tried different methods which I've seen in youtube tutorials but all of them show me the same error:
Cannot resolve symbol 'activity_menu'.
Does anyone know how to solve it?
Here is my code:
public class Menu extends AppCompatActivity {
Button boton_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
boton_start=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(activity_menu.this,activity_dishes.class);
startActivity(in);
}
});
}
}
The same happens with the other activity, but I suppose that the solution is the same for both.

You've mentioned the layout file names instead of name of the activities
Your code:
Intent in = new Intent(activity_menu.this,activity_dishes.class);
It should be:
Intent in = new Intent(Menu.this,Dishes.class);
Mention the name of the activity, not layout files.

Try getting activity_menu like this: R.layout.activity_menu

Related

How to disable On Click Listener created in onCreate, in onPause? (In Android Studio)

In Android Studio, I am trying to open the second activity when corresponding button is pressed.However, I cannot reach that listener that I create in "onCreate" from onPause. I am following an approach like this:
public class MainActivity extends Activity {
private View.OnClickListener openSecondPage = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setText("Clicked");
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setOnClickListener(openSecondPage);
}
public void onPause(){
super.onPause();
Button button_newPage = findViewById(R.id.button_newpage);
//Destroy the on click listener
button_newPage.setOnClickListener(null);
}
}
Also user will be able to come back to main activity and then go back to the second activity again. In that case I don't want to open a new activity. Instead I want to open previously created activity. For that case should I create a onResume() method and in that, call startActivity(secondPage). But in that case, since the secondPage is declared in onStart I won't be able to use in onResume. How can I handle that situation?
So there are actually 2 questions.. sorry about that, I didn't want to open 2 different questions for it.
Put Button button_newPage = findViewById(R.id.button_newpage); and button_newPage.setOnClickListener(openSecondPage); inside onResume instead of onCreate, like so:
#Override
protected void onResume() {
super.onResume();
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setOnClickListener(openSecondPage);
}
That should solve at least part of your problem.

How to save listview item checked

I'm using a listview to display grocery items with checkmark using android.R.layout.simple_list_item_checked. The problem is when i check an item and go to the previous page and comeback to the list my item is not checked anymore..i would like to use something else than sharedpreference is that possible and how can i do that?
I try to use SparseBooleanArray but it didnt work i probably use it wrong.
Since i use the previous phone button and after i click on the button that call my list activity i should use something in my onCreate method but i'm not sure what and how?
I also try to use oninstanceResore method like some people suggest me but i dont thin it what i'm looking for let say im on the activity 2 which is my item list with some item checked i decide to go back do something else using the phone previous button and then come back to the activity 2 using the button in my apps ...i want my item to still be checked. Can someone help me on how to do this i will be very grateful…
public class FruitList_Activity extends AppCompatActivity {
private ListView fruitsList;
private ArrayAdapter<String> adapterFruit;
private Button btn_Delete;
private Button btn_SelectAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_fruit_list_);
fruitsList = findViewById(R.id.list_Fruits);
fruitsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
btn_Delete = findViewById (R.id.btn_delete);
CreateActivity.itemsFruit = FileHelper.readData(this);
adapterFruit = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, CreateActivity.itemsFruit);
fruitsList.setAdapter(adapterFruit);
/*Remove items*/
btn_Delete.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
SparseBooleanArray checkedItemPositions = fruitsList.getCheckedItemPositions();
int itemCount = fruitsList.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
fruitsList.setItemChecked(i,true);
adapterFruit.remove(CreateActivity.itemsFruit.get(i));
FileHelper.writeData(CreateActivity.itemsFruit, FruitList_Activity.this );
}
}
adapterFruit.notifyDataSetChanged();
}
});
}
}
Can you guide me on how i can do that i'm just starting with java so please explain me so i can understand. thanks!
You need to create a Bundle with the information of the list, and before calling the second activity, you'll set the data inside that bundle, and when you came back to the main activity just check the bundle and if is different at null is because you need to load the data.
I Hope this cal help you :D
this is saved Instance()
also, this cal help you when you have onConfigurationChanged()
Save your adapterFruit using onSaveInstanceState
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("adapter",(Serializable) adapterFruit);
}
The onRestoreInstance executes when you go back to the activity
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(saveInstanceState != null){
adapterFruit = (ArrayAdapter<Adapter> )savedInstanceState.getSerializable("adapter");
}
}
Hope it helps.
you can do like this
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.checkBox.isChecked()==true) {
holder.checkBox.setChecked(true);
model_image.get(position).setSelected(true);
}
else
{
holder.checkBox.setChecked(false);
model_image.get(position).setSelected(false);
}
}
});

Attempt to write to null array android studio

I've created a simple 'app' to put values from text to an array and it doesn't seem to work.. I don't know why..
public class Register extends AppCompatActivity {
String[] data = new String[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
gotoReg();
}
public void gotoReg() {
Button register = (Button) findViewById(R.id.Submit);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
data[0] = "ss";
data[1] = "dd";
}
}
);
}
}
From this code I get the error :
java.lang.NullPointerException: Attempt to write to null array
I did almost the same code in another app and it worked.. seems really wierd to me...
Nevermind, thanks everyone, just re-opened Android studio and everything worked fine.
Wasted on it several hours ughh
Just put your code to get button reference after setContent view then,you can assign the values.
And you have checked the id of button?
Try to add
data = new String[2];
to your gotoReg() method, before setting the onClick listener.

ZXING Android Embedded Usage of IntentIntegrator

I am new to Android development and still trying to get the grasp of some of the concepts. I find the best way to learn is to jump right into the deep end with a project. With that said, here is my question:
I have integrated ZXing Android Embedded into my application; however, I am having trouble understanding the way in which you use IntentIntegrator. All I am trying to do at the moment is call the QR scanner to the screen when the user taps a button. I have been trying to follow the instructions on their github link [here][1] but have been unsuccessful.
Here is what my function looks like so far:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(this).initiateScan();
}
});
I keep getting an error saying:
Error:(109, 25) error: constructor IntentIntegrator in class
IntentIntegrator cannot be applied to given types; required: Activity
found: Intent reason: actual argument Intent cannot be converted to
Activity by method invocation conversion
Also, as I put my mouse over '(this)' in Android Studio, it says:
anonymous android.view.View.onClickListener
Any help would be greatly appreciated, thanks! If you need any other information, please let me know.
An even easier solution than ChrisStillwell´s would be to make your activity-/fragment-class implement the OnClickListener, this way you do not need the reference variable:
public class SomeFragment extends Fragment implements View.OnClickListener {
// Rest of your code
#Override
public void onClick(View v) {
if (v.getId == button.getId) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
IntentIntegrator.forFragment(this).initiateScan();
}
}
}
If you are implementing a fragment-class, note that you have to call getActivity() when creating the IntentIntegrator.
You are referencing your OnClickListener and not your Fragment when you say this inside your onClick. You need to reference your Fragment, the easiest way to do that is setup a global variable and call it myFragment or something to your liking, then assign it this. For example:
Fragment myFragment = this;
public void myFunction(){
// Code and stuff //
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
IntentIntegrator integrator = new IntentIntegrator(this);
IntentIntegrator.forFragment(myFragment).initiateScan();
}
});
}
following can work:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new IntentIntegrator(getActivity()).initiateScan();
popupWindow.dismiss();
}
});

An app that opens a PDF file on click?

Is there a way to open a PDF file with the click of a button.
The code I am using below downloads a PDF file from the web, it would be much easier if the PDF file was already on the app when it was download from the play store so the user doesn't have to download anything else.
I've already looked at this question and answer but i cannot make sense of it https://stackoverflow.com/review/suggested-edits/6350919 .
If its possible could you post an example as i am new to programming and find it difficult to make sense of what people are saying sometimes.
Thank you very much!
Here's my Code:
public class GreekHl extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greek_hl);
Button ge1 = (Button) findViewById(R.id.ge1);
ge1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
String url1 = "http://www.examinations.ie/archive/exampapers/2014/JC007ALP000EV.pdf";
Intent ge1 = new Intent(Intent.ACTION_VIEW);
ge1.setData(Uri.parse(url1));
startActivity(ge1);
}
});
Button gm1 = (Button) findViewById(R.id.gm1);
gm1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
String url1 = "http://examinations.ie/archive/markingschem/2014/JC007ALP000EV.pdf";
Intent ge1 = new Intent(Intent.ACTION_VIEW);
ge1.setData(Uri.parse(url1));
startActivity(ge1);
}
});
ps Im quite new to the whole coding thing so i may be a bit slow!
I think you'll also want to go:
ge1.setType("application/pdf");
So that when you start the activity, it'll redirect to PDF viewers.

Categories

Resources