I need some help in my application. I have these classes:
Ajuda.class:
public class Ajuda extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajuda);
}
}
testes.class:
public class teste extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnuAjuda:
Intent i = new Intent (this.getApplicationContext(),
Ajuda.class);
this.startActivity(i);
return true;
default: return super.onOptionsItemSelected(item);
}
}
}
On method onOptionsItemSelected in case R.id.mnuAjuda: if I got
Intent i = new Intent (this.getApplicationContext(),
Ajuda.class);
this.startActivity(i);
instead of
setContentView(R.layout.ajuda);
the application stop unexpectdly and I dont know why.
Did you put something like
<activity android:name=".Ajuda" android:label="Ajuda" />
in AndroidManifest.xml?
Related
Here is MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
Here is the menu XML file
<?xml version="1.0" encoding="utf-8"?>
<item android:title="#string/toast"
android:id="#+id/toast"/>
<item android:title="#string/nutella"
android:id="#+id/nutella"/>
<item android:title="#string/milk"
android:id="#+id/milk"/>
I want to handle myTxt which is a textview whenever I choose an item from the menu option the problem is that myTxt is not recognized. How Should I handle Menus correctly and what are
Here is an image for more explanation
You need to declare your TextView myTxt as global outside onCreate() method
SAMPLE CODE
public class MainActivity extends AppCompatActivity {
TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
myTxt is declared inside onCreate() so you can not access it from outside this function. Try declare it outside onCreate()
public class MainActivity extends AppCompatActivity {
private TextView myTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTxt = findViewById(R.id.view);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.toast:
myTxt.setText("what a yummy toast!");
return true;
case R.id.nutella:
return true;
case R.id.milk:
default:
return super.onOptionsItemSelected(item);// ken jét this .onOption infinite loop
}
}
}
how to save the text given to edit text, after clicking the save action button in action bar that should save to another activity.
my first activity Add.java
public class Add extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_add_action, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:save();
return true;
case R.id.cancel:cancelnavi();
return true;
}
return super.onOptionsItemSelected(item);
}
private void save() {
EditText titlename;
titlename=(EditText)findViewById(R.id.edittitle);
String title=titlename.getText().toString();
if (title.equalsIgnoreCase("")){
Toast.makeText(Add.this, "Script title should not be empty", Toast.LENGTH_LONG).show();
} else {
Intent i;
i=new Intent(getApplicationContext(), Scripts.class);
i.putExtra("n", titlename.getText().toString());
startActivityForResult(i, 0);
titlename.setText("");
}
}
}
It should be saved to scripts.java
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate=getMenuInflater();
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:editscript();
break;
}
return true;
}
private void editscript() {
Intent editinIntent;
editinIntent=new Intent(Scripts.this, Edit.class);
startActivity(editinIntent);
}
}
Maybe this can help you.
public class Scripts extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scripts);
getActionBar().setDisplayShowHomeEnabled(false);
//Extract the data
String yourTitle = getIntent().getStringExtra("n");
}
}
I have an activity, say MainActivity that has just onCreate method (no onResume, onStop, etc)
when a contBut is clicked, another activity, say SecondActivity is started and with pressing exitBut, we navigate back to MainActivity. in this case, onCreate of MainActivity is recalled which I can't figure why. I expect it to resume the activity, or at least not to call onCreate.
here is the code for MainActivity:
public class MainActivity extends Activity {
private Button contBut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contBut = (Button) findViewById(R.id.cont);
contBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("d", "in count onclick");
Intent intent = new Intent(MainActivity.this, ContActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
here is the code for SecondActivity:
public class SecondActivity extends Activity{
private Button exitBut;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cont);
exitBut = (Button) findViewById(R.id.exit);
exitBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
problem solved.
in the developer options section of my device, "Don't keep activities" was checked.
How can I remove a item from a ListActivity from a button on another Activity, the thing is,
I have this ListActivity:
public class ListaEventos extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onRestart();
republicar();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lista_eventos, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.criar_evento:
Intent criar = new Intent(this, CriarEvento.class);
startActivity(criar);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void republicar() {
List<DadosEvento> eventos = MySingleton.getInstance().getEventos();
setListAdapter(new ArrayAdapter<DadosEvento>(this, android.R.layout.simple_list_item_1, eventos));
}
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
DadosEvento clickNumber = MySingleton.getInstance().getEventos().get(position);
Intent intent = new Intent(this, ExibirEvento.class);
Bundle exibirEvento = new Bundle();
exibirEvento.putString("exibirNome", clickNumber.getNome());
exibirEvento.putString("exibirData", clickNumber.getData());
exibirEvento.putString("exibirEnd", clickNumber.getEndereco());
exibirEvento.putString("exibirTel", clickNumber.getTel());
intent.putExtras(exibirEvento);
startActivity(intent);
}
}
Other Activity:
public class ExibirEvento extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exibir_evento);
Intent intent = getIntent();
Bundle exibirEvento = intent.getExtras();
String exibirNome = exibirEvento.getString("exibirNome");
String exibirData = exibirEvento.getString("exibirData");
String exibirEnd = exibirEvento.getString("exibirEnd");
String exibirTel = exibirEvento.getString("exibirTel");
TextView exibir_nome = (TextView) findViewById(R.id.exibirevento_edittext_nome);
exibir_nome.setText(exibirNome);
TextView exibir_data = (TextView) findViewById(R.id.exibirevento_edittext_data);
exibir_data.setText(exibirData);
TextView exibir_end = (TextView) findViewById(R.id.exibirevento_edittext_end);
exibir_end.setText(exibirEnd);
TextView exibir_tel = (TextView) findViewById(R.id.exibirevento_edittext_tel);
exibir_tel.setText(exibirTel);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exibir_evento, menu);
return true;
}
public void sairExibicao(View v) {
finish();
}
}
Now, on the ExibirEvento is where it shows me the infos about the Party, and there I need a button that removes that item from the List.
Well there are several scenarios that you can follow.
Use startActivityForResult instead of startActivity and then override the onActivityResult method and catch a proper requestCode and result (e. g. RESULT_OK) and delete current item and notifyDataChanged the adapter
Use singelton class, create newInstance, set values, and then refere to it from ExibirEvento
Use the database, and take advantage of ContentProvider and Loader, and pass only the ID of the entry to ExibirEvento
I think that the 3 option is the best way to go for.
I have one problem using ActivityGroup. I have two activities inside an ActivityGroup and both of them use a menu (overriding the onCreateOptionMenu and onOptionsItemSelected).
Both activity have different menus.
Well, the problem is that the second activity always show the first activity menu,
Any idea about this issue?
Below is my code
public class myActivityGroup extends ActivityGroup {
----
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
View view = window.getDecorView();
history.add(view);
setContentView(view);
}
}
public void back() {
if (history.size() > 0) {
int lastActivityIndex = history.size() - 1;
int lastIDIndex = mIdList.size() - 1;
String activityId = mIdList.get(lastIDIndex);
Log.d(TAG, "activityId:" + activityId);
history.remove(lastActivityIndex);
mIdList.remove(lastIDIndex);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Activity current = getLocalActivityManager().getCurrentActivity();
return current.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
LocalActivityManager manager = getLocalActivityManager();
Activity current = manager.getCurrentActivity();
return current.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LocalActivityManager manager = getLocalActivityManager();
Activity current = manager.getCurrentActivity();
return current.onOptionsItemSelected(item);
}
}
public class ChildActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//add menu here
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//handle on menu item selected here
return true;
}
}
public class ChildActivity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//add menu here
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.offer_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//handle on menu item selected here
return true;
}
}
When you add the activities, the "current" activity is that last one added. I suspect that if you interact with the other activity then activate the options menu it will work.
Try retrieving the activity you need using the String Id:
LocalActivityManager manager = getLocalActivityManager();
Activity a = manager.getActivity(id);
return a.onCreateOptionsMenu(menu);