gpt4 book ai didi

android - 找不到使用 android :onclick 的方法

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:43 24 4
gpt4 key购买 nike

我在 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" />

我的 Activity :

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): FATAL EXCEPTION: main
04-09 17:36:28.080: E/AndroidRuntime(536): java.lang.IllegalStateException: Could not find a method myFunction(View) in the activity class com.finalProject.uniNote.Main for onClick handler on view class android.widget.ImageButton with id 'delete_img'

最佳答案

假设您的按钮在您的 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");
}

}

关于android - 找不到使用 android :onclick 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com