我创建了一个简单的Android应用程序来显示吐司,但每当我尝试单击该按钮时,应用程序崩溃
我的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.nsk.demoapp.add_layout"
tools:showIn="@layout/add_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hi there"
android:textSize="60dp"
android:gravity="center" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm"
android:gravity="center"
android:onClick="toastNote"/>
</RelativeLayout>
</LinearLayout>
我的java代码
package com.example.nsk.demoapp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class add_layout extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void toastNote(View v)
{
Toast.makeText(this, "Hi there", Toast.LENGTH_LONG).show();
}
}
Android监视器错误
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nsk.demoapp, PID: 3531
java.lang.IllegalStateException: Could not find method toastNote(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'bt1'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
使用getApplicationContext()
代替this
。所以你的吐司应该是这样的:
Toast.makeText(getApplicationContext(), "Hi there", Toast.LENGTH_LONG).show();
而不是:
Toast.makeText(this, "Hi there", Toast.LENGTH_LONG).show();
希望这有帮助!
答案 1 :(得分:0)
进行以下更改
public void toastNote(View bt1)<----- bt1 is important as u define onClick in xml
{
Toast.makeText(this, "Hi there", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
您可以使用YourClassName.this
Toast.makeText(YourClassName.this, "TEXT", Toast.LENGTH_SHORT).show();
或getApplicationContext()
Toast.makeText(getApplicationContext(), "TEXT", Toast.LENGTH_SHORT).show();