当我单击按钮时,Android 应用程序崩溃,onClickListener 出现问题

戴夫95

我正在创建一个简单的 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) 

关于我在声明我的听众时做错了什么的任何建议都会很棒,因为它在我的其他班级中工作得很好。

谢谢一堆。

conman124

在您的 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.
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android:西班牙语:解析浮点值时出现问题:应用程序崩溃

来自分类Dev

我的winsock应用程序出现问题

来自分类Dev

当我单击按钮时,应用程序崩溃

来自分类Dev

当我尝试设置onClickListener时,应用程序崩溃

来自分类Dev

RelativeLayout onClickListener使我的应用程序崩溃

来自分类Dev

应用应用模板时出现问题

来自分类Dev

我的Chrome扩展程序出现问题

来自分类Dev

“ Hello World” Android应用出现问题

来自分类Dev

当我使用OnClicklistener作为按钮时,即使它为空,我的应用程序也会崩溃

来自分类Dev

当我单击菜单按钮时,Android 应用程序崩溃

来自分类Dev

在Google OAuth 2.0上注册我的应用程序API时出现问题

来自分类Dev

在我的Rails应用程序中为用户创建汽车时出现问题

来自分类Dev

从我的应用程序更新Facebook用户状态时出现问题

来自分类Dev

在我的应用程序中显示特定的通知消息时出现问题

来自分类Dev

Xcode:将我的应用程序添加到AppStore时出现问题

来自分类Dev

创建我的第一个android应用程序时出现问题

来自分类Dev

在为Android应用程序实现快捷方式时出现问题

来自分类Dev

在Android应用程序中使用Facebook登录时出现问题

来自分类Dev

使用Appium在Android本机应用程序中滚动时出现问题

来自分类Dev

使用Android 10在pdf应用程序上显示pdf文件时出现问题

来自分类Dev

单击时,TextView onClickListener 使应用程序崩溃

来自分类Dev

我的宏在Common Lisp中的部分应用程序出现问题

来自分类Dev

是什么导致我的WIX安装的应用程序出现问题?

来自分类Dev

在F-Droid中发布我的应用程序时出现问题

来自分类Dev

我的宏在Common Lisp中的部分应用程序出现问题

来自分类Dev

Citrix直接登录我的应用程序时出现问题

来自分类Dev

在我的离子应用程序中使用Google Maps API出现问题

来自分类Dev

在WP8.1应用程序中滚动和缩放图像时出现问题

来自分类Dev

运行`foreman start` /将Django应用程序部署到Heroku时出现问题

Related 相关文章

  1. 1

    Android:西班牙语:解析浮点值时出现问题:应用程序崩溃

  2. 2

    我的winsock应用程序出现问题

  3. 3

    当我单击按钮时,应用程序崩溃

  4. 4

    当我尝试设置onClickListener时,应用程序崩溃

  5. 5

    RelativeLayout onClickListener使我的应用程序崩溃

  6. 6

    应用应用模板时出现问题

  7. 7

    我的Chrome扩展程序出现问题

  8. 8

    “ Hello World” Android应用出现问题

  9. 9

    当我使用OnClicklistener作为按钮时,即使它为空,我的应用程序也会崩溃

  10. 10

    当我单击菜单按钮时,Android 应用程序崩溃

  11. 11

    在Google OAuth 2.0上注册我的应用程序API时出现问题

  12. 12

    在我的Rails应用程序中为用户创建汽车时出现问题

  13. 13

    从我的应用程序更新Facebook用户状态时出现问题

  14. 14

    在我的应用程序中显示特定的通知消息时出现问题

  15. 15

    Xcode:将我的应用程序添加到AppStore时出现问题

  16. 16

    创建我的第一个android应用程序时出现问题

  17. 17

    在为Android应用程序实现快捷方式时出现问题

  18. 18

    在Android应用程序中使用Facebook登录时出现问题

  19. 19

    使用Appium在Android本机应用程序中滚动时出现问题

  20. 20

    使用Android 10在pdf应用程序上显示pdf文件时出现问题

  21. 21

    单击时,TextView onClickListener 使应用程序崩溃

  22. 22

    我的宏在Common Lisp中的部分应用程序出现问题

  23. 23

    是什么导致我的WIX安装的应用程序出现问题?

  24. 24

    在F-Droid中发布我的应用程序时出现问题

  25. 25

    我的宏在Common Lisp中的部分应用程序出现问题

  26. 26

    Citrix直接登录我的应用程序时出现问题

  27. 27

    在我的离子应用程序中使用Google Maps API出现问题

  28. 28

    在WP8.1应用程序中滚动和缩放图像时出现问题

  29. 29

    运行`foreman start` /将Django应用程序部署到Heroku时出现问题

热门标签

归档