我正在创建一个简单的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)
任何关于在宣布我的听众时我做错了什么的建议都会很棒,因为它在我的其他课堂上运作得很好。
非常感谢。
答案 0 :(得分:0)
在您的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.
}
}