我在列表视图中有一个带有图像按钮的 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");
}
}
日志输出:
04-09 17:36:28.080: E/AndroidRuntime(536): 致命异常: main
04-09 17:36:28.080: E/AndroidRuntime(536): java.lang.IllegalStateException: 找不到方法 myFunction(View ) 在活动类 com.finalProject.uniNote.Main 中,用于视图类 android.widget.ImageButton 上的 onClick 处理程序,id 为“delete_img”