So I have a menu inflator and when I select something in it, my program force closes and I believe it is because the class also implements onclicklistener for an array of buttons I have to add. Here is some of the relevant code:
package com.riley.howmany;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class howMany extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//I have to use a dynamic layout because it changes based on user options.
//As of right now it is just in a for loop because the settings menu won't open
//because of this issue. I hope this makes sense.
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
ll.setPadding(1,1,1,1);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
//Each button press actually performs the same code for that individual button
for (int c=0; c<=10; c++) {
Button b = new Button (this);
b.setText("Button:"+" "+"0");
b.setTextSize(10.0f);
b.setOnClickListener(this);
ll.addView(b);
}
this.setContentView(sv);
}
public void onClick(View view) {
//handle each button click
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, Setting.class);
startActivity(intent);
return true;
}
}
Thank you very much for any advice you can provide!
EDIT/UPDATE!
Ok I found the fix. Here it is:
@Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
startActivity(new Intent(this, Setting.class));
return true;
}
Now it works great! Thank you for the help.
Your onClick listener shouldn’t be the problem.
The force close can come from your startActivity() call.
Are you sure you have declared the Setting activity in your manifest?
Could you post your whole manifest?