0

トーストが表示されないというよくある問題があります。サイトで何度も検索し、さまざまなことを試しました。ただし、まだ機能しません。

私のコード:

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も試しました。thisgetApplicationContext()

コードの結果は画面を開き、ボタンを押した後、何も起こりませんでした。

他に選択肢がないので、皆さんに助けを求めます。

前もって感謝します、

ヤニフ。

@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
4

4 に答える 4

2

シミュレーターを使用していますか、それともデバイスで実行していますか?

何も問題はないようですが、これがうまくいくかどうか試してみてください。同じですが、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();

}
于 2014-01-25T17:55:55.430 に答える
0

このActivity ContextとApplication Contextの違いによると、 getApplicationContext()ではなくActivity.thisを使用する必要があります

于 2014-01-25T17:04:22.437 に答える