我正在尝试了解听众在Android中的工作方式。为此,我创建了一个简单的应用程序,其中包含2个活动,其中主要活动“监听”第二个活动。该应用程序崩溃。
由于整个应用程序非常小,我将在此处发布整个内容以及logcat。
我的问题是:我做错了什么以及如何让它发挥作用。
MainActivity :(这会侦听Listen活动)
public class MainActivity extends Activity implements Listen.Listener {
Listen l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = new Listen();
l.setListener(this);
}
public void onButton(final View v)
{
Intent i = new Intent(this, Listen.class);
startActivity(i);
}
@Override
public void withoutMessage()
{
Toast.makeText(this, "click received in the main activity class", Toast.LENGTH_SHORT).show();
}
}
聆听活动:
public class Listen extends Activity {
public interface Listener {
public void withoutMessage();
}
Listener listner = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listen);
}
public void setListener(Listener l) {
listner = l;
}
public void onButton(final View v)
{
Log.w("Rakshak", "button click");
Toast.makeText(this, "button clicked from the the Listen class", Toast.LENGTH_SHORT).show();
listner.withoutMessage();
}
}
LogCat:
05-30 11:20:50.764: E/AndroidRuntime(4726): FATAL EXCEPTION: main
05-30 11:20:50.764: E/AndroidRuntime(4726): java.lang.IllegalStateException: Could not execute method of the activity
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.view.View$1.onClick(View.java:3607)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.view.View.performClick(View.java:4212)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.view.View$PerformClick.run(View.java:17476)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.os.Handler.handleCallback(Handler.java:800)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.os.Handler.dispatchMessage(Handler.java:100)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.os.Looper.loop(Looper.java:194)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.app.ActivityThread.main(ActivityThread.java:5371)
05-30 11:20:50.764: E/AndroidRuntime(4726): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 11:20:50.764: E/AndroidRuntime(4726): at java.lang.reflect.Method.invoke(Method.java:525)
05-30 11:20:50.764: E/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-30 11:20:50.764: E/AndroidRuntime(4726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-30 11:20:50.764: E/AndroidRuntime(4726): at dalvik.system.NativeStart.main(Native Method)
05-30 11:20:50.764: E/AndroidRuntime(4726): Caused by: java.lang.reflect.InvocationTargetException
05-30 11:20:50.764: E/AndroidRuntime(4726): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 11:20:50.764: E/AndroidRuntime(4726): at java.lang.reflect.Method.invoke(Method.java:525)
05-30 11:20:50.764: E/AndroidRuntime(4726): at android.view.View$1.onClick(View.java:3602)
05-30 11:20:50.764: E/AndroidRuntime(4726): ... 11 more
05-30 11:20:50.764: E/AndroidRuntime(4726): Caused by: java.lang.NullPointerException
05-30 11:20:50.764: E/AndroidRuntime(4726): at com.example.listnertest.Listen.onButton(Listen.java:35)
05-30 11:20:50.764: E/AndroidRuntime(4726): ... 14 more
答案 0 :(得分:2)
此
l = new Listen();
是错误的,因为它是一个Activity类。永远不要实例化Activity类。 Activity类不是普通的java类。它有一个生命周期,它在清单文件中声明。
您的listner
为空。
答案 1 :(得分:1)
你不能只是实例化一个Activity类..那个错误来自..它将返回一个null ..
溶液:
只需创建一个界面,而不是活动,你可以创建一个片段..它将听你的主要活动