未找到Android按钮onClick处理程序

时间:2015-07-05 22:33:57

标签: java android xml button intellij-idea

这个问题似乎很普遍,我看过其他类似的问题,但没有一个问题对我有所帮助。

我有一个带有按钮的片段的活动。单击按钮时出现以下错误,我不知道原因:

java.lang.IllegalStateException: Could not find a method onSaveIdeaButtonClick(View) in the activity class com.zarwanhashem.ideatrackr.EditIdeaPageActivity for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'saveIdeaButton'

但是当我控制+单击IntelliJ中XML中的onClick属性时,它会正确地转到活动中的onClick方法。

这是按钮xml:

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save_edit_bttn"
        android:id="@+id/saveIdeaButton"
        android:onClick="onSaveIdeaButtonClick"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

这是EditIdeaPageActivity类:

public class EditIdeaPageActivity extends AppCompatActivity {
    private static final String IDEA_TITLE_KEY = "title";
    private static final String IDEA_DETAILS_KEY = "details";

    private Context myContext;
    private static SharedPreferences sharedPref;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_idea_page);
        myContext = getApplicationContext();

        Intent intent = getIntent();
        EditText ideaTitle = (EditText)findViewById(R.id.ideaTitle);
        EditText ideaDetails = (EditText)findViewById(R.id.ideaDetails);
        sharedPref = myContext.getSharedPreferences("sharedPref", 0);

        if (intent != null) {
            setTitle(intent.getStringExtra("title"));
            ideaTitle.setText(intent.getStringExtra(IDEA_TITLE_KEY));
            ideaDetails.setText(intent.getStringExtra(IDEA_DETAILS_KEY));
        } else {
            setTitle("Error - no idea provided to edit");
            ideaTitle.setText("Error");
            ideaDetails.setText("Error");
            id = intent.getIntExtra("ID", -1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void onSaveIdeaButtonClicked(View v) {
        SharedPreferences.Editor editor = sharedPref.edit();
        EditText ideaTitle = (EditText)findViewById(R.id.ideaTitle);
        EditText ideaDetails = (EditText)findViewById(R.id.ideaDetails);

        editor.putString(IDEA_TITLE_KEY, ideaTitle.getText().toString());
        editor.putString(IDEA_DETAILS_KEY, ideaDetails.getText().toString());
        editor.commit();

        Intent intent = new Intent(v.getContext(), MainActivity.class);
        intent.putExtra(IDEA_TITLE_KEY, ideaTitle.getText().toString());
        intent.putExtra(IDEA_DETAILS_KEY, ideaDetails.getText().toString());
        intent.putExtra("Edit", true);
        intent.putExtra("ID", id);
        startActivity(intent);
    }
}

为什么说无法找到该方法?如果需要更多细节,请告诉我。

1 个答案:

答案 0 :(得分:1)

您的xml:android:onClick="onSaveIdeaButtonClick",您的方法为public void onSaveIdeaButtonClicked(View v)

更改您的方法:

public void onSaveIdeaButtonClick(View v)