I'm creating a simple android application and was moving along nicely until i created a new class and attempted to implement two buttons.
I simply cannot get my head around why they aren't working/initialising correctly.
Here is my code:
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();
}
}
Here is the corresponding XML file:
<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>
And upon running my app and hitting the button i get the following error:
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)
Any advice as to what i'm doing wrong when declaring my listener would be great because it works just fine in my other class.
Thanks a bunch.
In your xml you have said that it should call the next
method when the button is clicked:
<Button
android:id="@+id/buttonNext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="next"/> <!-- here -->
But you don't have a next
method declared on your activity. Just add the method to your activity:
public class CreateSurveyActivity extends Activity {
...
public void next(View buttonView) {
// Whatever should happen when next is clicked.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.