这是我的问题。我创建了一个按钮ID:“fibo1”。此按钮属于“CList”活动。点击按钮后,我想打开另一个名为“斐波那契”的活动
'CList'的XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/fibo1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Fibo" />
CList的Java
public class CList extends Activity {
Button read1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
read1 = (Button) findViewById (R.id.fibo1);
read1.setOnClickListener (new OnClickListener(){
@Override
public void onClick (View v){
Intent intent = new Intent (CList.this,Fibonacci.class);
startActivity (intent);
finish();
}
});
}
应用程序在从主活动打开“列表”活动时崩溃。 Log Cat提供错误Fatal Exception :main
。
我已将这些活动添加到Android Manifest。
我只是一个初学者,如果我的术语不正确,请原谅我。
这是Log Cat
03-07 19:33:29.931: E/AndroidRuntime(19565): FATAL EXCEPTION: main
03-07 19:33:29.931: E/AndroidRuntime(19565): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geek.cprogramspopular/com.geek.cprogramspopular.CList}: java.lang.NullPointerException
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.access$700(ActivityThread.java:150)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.os.Looper.loop(Looper.java:137)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.main(ActivityThread.java:5283)
03-07 19:33:29.931: E/AndroidRuntime(19565): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 19:33:29.931: E/AndroidRuntime(19565): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 19:33:29.931: E/AndroidRuntime(19565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-07 19:33:29.931: E/AndroidRuntime(19565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-07 19:33:29.931: E/AndroidRuntime(19565): at dalvik.system.NativeStart.main(Native Method)
03-07 19:33:29.931: E/AndroidRuntime(19565): Caused by: java.lang.NullPointerException
03-07 19:33:29.931: E/AndroidRuntime(19565): at com.geek.cprogramspopular.CList.onCreate(CList.java:23)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.Activity.performCreate(Activity.java:5283)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
03-07 19:33:29.931: E/AndroidRuntime(19565): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-07 19:33:29.931: E/AndroidRuntime(19565): ... 11 more
答案 0 :(得分:0)
您只需从XML文件中删除此行,您无需在Intent Calling中完成您的活动......
android:onClick="sendMessage"
read1.setOnClickListener (new OnClickListener(){
@Override
public void onClick (View v){
Intent intent = new Intent (CList.this,Fibonacci.class);
startActivity (intent);
}
});
答案 1 :(得分:0)
使用以下代码 -
public void sendMessage(View v) {
Intent intent = new Intent(CList.this, Fibonacci.class);
startAcitivty(intent);
}
在onCreate(Bundle savedInstanceState)
方法之后,而不是 -
read1.setOnClickListener (new OnClickListener(){
@Override
public void onClick (View v){
Intent intent = new Intent (CList.this,Fibonacci.class);
startActivity(intent);
finish();
}
执行此操作后,您也可以删除Button read1;
和read1 = (Button) findViewById (R.id.fibo1);
。