簡體   English   中英

當我單擊按鈕時,Android App崩潰,onClickListener問題

[英]Android App crashing as i click button, issue with onClickListener

我正在創建一個簡單的android應用程序,並且進展順利,直到我創建了一個新類並嘗試實現兩個按鈕。

我根本無法理解為什么它們無法正常工作/初始化。

這是我的代碼:

public class CreateSurveyActivity extends Activity {

EditText editQuestion, editAnswer1, editAnswer2, editAnswer3;
Button btnNext, btnComplete;
SurveyDataBaseAdapter SurveyDataBaseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.photoalbum);

    // get Instance  of Database Adapter
    SurveyDataBaseAdapter = new SurveyDataBaseAdapter(this);
    SurveyDataBaseAdapter = SurveyDataBaseAdapter.open();

    // Get References of Views
    editQuestion = (EditText) findViewById(R.id.editTextQuestion);
    editAnswer1 = (EditText) findViewById(R.id.editTextAnswer1);
    editAnswer2 = (EditText) findViewById(R.id.editTextAnswer2);
    editAnswer3 = (EditText) findViewById(R.id.editTextAnswer3);

    // Get Refs of buttons


    btnComplete = (Button) findViewById(R.id.buttonSignUP);
}


 public void createSurvey(){

    final Dialog dialog = new Dialog(CreateSurveyActivity.this);
    dialog.setContentView(R.layout.photoalbum);
    dialog.setTitle("Create");

    // get the Refferences of views
    final  EditText editQuestion=(EditText)dialog.findViewById(R.id.editTextQuestion);
    final  EditText editAnswer1=(EditText)dialog.findViewById(R.id.editTextAnswer1);
    final  EditText editAnswer2=(EditText)dialog.findViewById(R.id.editTextAnswer2);
    final  EditText editAnswer3=(EditText)dialog.findViewById(R.id.editTextAnswer3);

     // Set On ClickListener .... Error here i presume.
     btnNext = (Button) dialog.findViewById(R.id.buttonNext);
     btnNext.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String question = editQuestion.getText().toString();
            String answer1 = editAnswer1.getText().toString();
            String answer2 = editAnswer2.getText().toString();
            String answer3 = editAnswer3.getText().toString();

            // check if any of the fields are vaccant
            if (question.equals("") || answer1.equals("") || answer2.equals("") || answer3.equals("")) {
                Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                return;
            }

            {
                // Save the Data in Database
                SurveyDataBaseAdapter.insertQuestion(question, answer1, answer2, answer3);
                Toast.makeText(getApplicationContext(), "Question Successfully Created ", Toast.LENGTH_LONG).show();
            }
        }
    });


    // try to send home on complete button press
    //*
    //btnNext = (Button) findViewById(R.id.buttonNext);
   // btnNext.setOnClickListener(new View.OnClickListener() {

      //  final Intent intent = new Intent(context, DashHomeActivity.class);
        //
   // });
}


@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    SurveyDataBaseAdapter.close();
}
}

這是相應的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout style="@style/TitleBar">
    <ImageButton style="@style/TitleBarOperation"
        android:src="@drawable/home_def"
        android:onClick="onHome"
        android:layout_marginTop = "5dip"
        android:layout_marginRight="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginBottom = "5dip"
        android:paddingBottom = "5dip"
        android:background="@drawable/bg_state"
        android:layout_gravity="center"
        android:paddingLeft="5dip"
        android:paddingRight="7dip"/>

    <ImageView android:layout_width="1px"
        android:layout_height="fill_parent"
        android:background="@drawable/separator"
        android:layout_marginRight="7dip"
        />

    <TextView style="@style/TitleBarText"
        android:paddingLeft="8dip"/>

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <TextView
        style="@style/TextBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dip"
        android:text="@string/photoAlbum_detail" />

    <EditText
        android:id="@+id/editTextQuestion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Question"
         />

    <EditText
        android:id="@+id/editTextAnswer1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Answer 1"
        />

    <EditText
        android:id="@+id/editTextAnswer2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Answer 2"
        />

    <EditText
        android:id="@+id/editTextAnswer3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Answer 3"
        />

    <Button
        android:id="@+id/buttonNext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:onClick="next"/>
    <Button
        android:id="@+id/buttonComplete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Complete"
        android:onClick="Complete"/>



</LinearLayout>

在運行我的應用程序並按下按鈕時,出現以下錯誤:

04-15 21:07:02.622 6642-6642/com.example.david.myview3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.david.myview3, PID: 6642
                                                                     java.lang.IllegalStateException: Could not find a method next(View) in the activity class com.example.david.myview3.CreateSurveyActivity for onClick handler on view class android.widget.Button with id 'buttonNext'
                                                                         at android.view.View$1.onClick(View.java:4013)
                                                                         at android.view.View.performClick(View.java:4785)
                                                                         at android.view.View$PerformClick.run(View.java:19869)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:155)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
                                                                      Caused by: java.lang.NoSuchMethodException: next [class android.view.View]
                                                                         at java.lang.Class.getMethod(Class.java:664)
                                                                         at java.lang.Class.getMethod(Class.java:643)
                                                                         at android.view.View$1.onClick(View.java:4006)
                                                                         at android.view.View.performClick(View.java:4785) 
                                                                         at android.view.View$PerformClick.run(View.java:19869) 
                                                                         at android.os.Handler.handleCallback(Handler.java:739) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                         at android.os.Looper.loop(Looper.java:155) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

關於在聲明我的聽眾時我做錯了什么的任何建議都是很好的,因為它在我的其他課堂上也很好。

謝謝你

在您的xml中,您曾說過單擊按鈕時應調用next方法:

<Button
    android:id="@+id/buttonNext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Next"
    android:onClick="next"/> <!-- here -->

但是您沒有在活動中聲明next方法。 只需將方法添加到您的活動中:

public class CreateSurveyActivity extends Activity {
    ...
    public void next(View buttonView) {
        // Whatever should happen when next is clicked.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM