我遇到了一个常见问题,关于我的 Toast 没有出现。我在网站上搜索了很多次,并尝试了一些不同的东西。但是 - 它仍然不起作用。
我的代码:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Welcome extends Activity implements OnClickListener {
EditText efn,eln,eage;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
efn=(EditText)findViewById(R.id.fname);
eln=(EditText)findViewById(R.id.lname);
eage=(EditText)findViewById(R.id.age);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
}
欢迎的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/welcome_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fname"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="26dp"
android:layout_marginTop="25dp"
android:text="Welcome" />
<EditText
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/welcome_text"
android:layout_marginLeft="26dp"
android:layout_marginTop="37dp"
android:ems="10"
android:hint="First name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fname"
android:layout_below="@+id/fname"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="Last name"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lname"
android:layout_below="@+id/lname"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Age"
android:textDirection="ltr"
android:inputType="number" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Submit" />
而不是Welcome.this
我也尝试过this
and getApplicationContext()
。
代码的结果是打开屏幕并按下按钮后 - 没有任何反应。
我没有其他选择..因此我向你们寻求帮助。
提前致谢,
亚尼夫。
根据@jmsalcido 回答的错误日志:
01-25 17:57:07.367: D/gralloc_goldfish(3248): Emulator without GPU emulation detected.
01-25 17:57:08.387: I/Choreographer(3248): Skipped 48 frames! The application may be doing too much work on its main thread.
01-25 17:57:08.768: D/AndroidRuntime(3248): Shutting down VM
01-25 17:57:08.768: W/dalvikvm(3248): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-25 17:57:08.798: E/AndroidRuntime(3248): FATAL EXCEPTION: main
01-25 17:57:08.798: E/AndroidRuntime(3248): java.lang.IllegalStateException: Could not find a method showToast(View) in the activity class com.example.idiagnostician1.MainActivity for onClick handler on view class android.widget.Button with id 'submit'
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.view.View$1.onClick(View.java:3586)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.view.View.performClick(View.java:4204)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.view.View$PerformClick.run(View.java:17355)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.os.Handler.handleCallback(Handler.java:725)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.os.Looper.loop(Looper.java:137)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-25 17:57:08.798: E/AndroidRuntime(3248): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 17:57:08.798: E/AndroidRuntime(3248): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 17:57:08.798: E/AndroidRuntime(3248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 17:57:08.798: E/AndroidRuntime(3248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 17:57:08.798: E/AndroidRuntime(3248): at dalvik.system.NativeStart.main(Native Method)
01-25 17:57:08.798: E/AndroidRuntime(3248): Caused by: java.lang.NoSuchMethodException: showToast [class android.view.View]
01-25 17:57:08.798: E/AndroidRuntime(3248): at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-25 17:57:08.798: E/AndroidRuntime(3248): at java.lang.Class.getMethod(Class.java:915)
01-25 17:57:08.798: E/AndroidRuntime(3248): at android.view.View$1.onClick(View.java:3579)
01-25 17:57:08.798: E/AndroidRuntime(3248): ... 11 more