我在listview中有一个带有图像按钮的ListFragment。在imageButton XML中,我有android:onclick,我在Main.java中有正确格式化的方法,但我仍然得到它无法找到方法的错误。有什么想法吗?
XML:
<ImageButton
android:id="@+id/delete_img"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:onClick="myFunction"
android:paddingRight="10dip"
android:paddingTop="3dip"
android:src="@drawable/delete_icon" />
我的活动:
public class Main extends Activity {
public static Context appContext;
private CommentsDataSource datasource;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
datasource = new CommentsDataSource(this);
datasource.open();
// returnNotes();
List<Comment> values = datasource.getAllComments(); // unnecessary?
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
// setListAdapter(adapter); this must be imported to listFragment
// notesfragment
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appContext = getApplicationContext();
// ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab NotesTab = actionbar.newTab().setText("Notes");
ActionBar.Tab FoldersTab = actionbar.newTab().setText("Folders");
ActionBar.Tab OptionsTab = actionbar.newTab().setText("Options");
Fragment NotesTest = new NotesFragment();
Fragment FoldersFragment = new FoldersFragment();
Fragment OptionsFragment = new OptionsFragment();
NotesTab.setTabListener(new MyTabsListener(NotesTest));
FoldersTab.setTabListener(new MyTabsListener(FoldersFragment));
OptionsTab.setTabListener(new MyTabsListener(OptionsFragment));
actionbar.addTab(NotesTab);
actionbar.addTab(FoldersTab);
actionbar.addTab(OptionsTab);
}
public List<Comment> returnNotes() {
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> theNotes = datasource.getAllComments();
return theNotes;
}
// Constructs the options menu with tabs
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onDestroy() {
datasource.close();
super.onDestroy();
}
// Tab event listener
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_note:
Intent intentnote = new Intent(this, NewNote.class);
startActivity(intentnote);
return true;
case R.id.new_folder:
Intent intentfolder = new Intent(this, NewFolder.class);
startActivity(intentfolder);
return true;
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
我的标签听众:
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// do something on tab reselected?
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
public void myFunction(View v) {
Log.d("uniNote", "hello world");
}
}
Logcat输出:
04-09 17:36:28.080:E / AndroidRuntime(536):致命异性:主要
04-09 17:36:28.080:E / AndroidRuntime(536):java.lang.IllegalStateException:无法在视图类android上的onClick处理程序的活动类com.finalProject.uniNote.Main中找到方法myFunction(View)。 widget.ImageButton,id为'delete_img'
答案 0 :(得分:4)
假设您的按钮在R.layout.main
中,您需要在实例化该布局的类中使用onClick
方法,在本例中为Main
类:
public class Main extends Activity {
public static Context appContext;
private CommentsDataSource datasource;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments(); // unnecessary?
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// etc
}
// Inside Main class - outside of onCreate
public void myFunction(View v) {
Log.d("uniNote", "hello world");
}
}