[英]Toast isn't working
我遇到一個常見的問題,關於我的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
和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
您是在使用模擬器還是在設備中運行模擬器?
似乎沒什么問題,但是嘗試一下,如果對您有用,它是相同的,但不是實現OnClickListener和重寫,請嘗試此操作
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
}
});
這樣離開班級
public class Welcome extends Activity
並刪除
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
}
您需要將此支票放入OnClick
if (v.getId() == R.id.submit) {
Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
}
當您實現OnClickListener
, onClick()
方法將使用View
。 在onClick()
執行任何操作時,我們需要檢查調用了哪個View
。 如果可能有多個按鈕(或單擊一個以上的視圖),這有助於確定我們將onCLick稱為哪個View
。 因此,檢查if (v.getId() == R.id.submit)
是否重要。
根據Activity Context和Application Context之間的區別,您應該使用Activity。而不是getApplicationContext()
經常去developers.android.com: http://developer.android.com/guide/topics/ui/controls/button.html#ClickListener
不要覆蓋onClick方法,在此使用onClick偵聽器,這應該可以工作,這是您的實際文件:
<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" />
在此處添加android:onClick屬性,如下所示:
<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:onClick="showToast"
android:text="Submit" />
並在您的活動中:
public void showToast(View v) {
Toast.makeText(getApplicationContext(), "Please complete all the details", Toast.LENGTH_SHORT).show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.