我开发了一个应用程序,用于从第一个活动到第二个活动以及从第二个活动到第三个活动的导航。当我运行我的应用程序时,它会从第1个活动导航到第2个活动,但不会从第2个活动导航到第3个活动,而是显示“不幸的是应用已停止”。我发布了我的代码和日志cat errors.help我请。从errors.plsss中恢复过来帮助我。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="REGISTER" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="54dp"
android:layout_toRightOf="@+id/button1"
android:text="LOGIN" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="23dp"
android:text="USER NAME"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="REGISTRATION FORM"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignRight="@+id/editText1"
android:ems="10"
android:inputType="textPassword" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="PASSWORD"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText2"
android:text="E-MAIL ID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView5"
android:ems="10"
android:inputType="textPostalAddress" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/editText3"
android:text="ADDRESS"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="SUBMIT" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText4"
android:layout_centerVertical="true"
android:text="CANCEL" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="16dp"
android:layout_y="238dp"
android:text="DELIVERY" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="4dp"
android:layout_y="141dp"
android:text="YOU HAVE SUCCESSFULLY LOGGED IN"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:context=".WelActivity" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="90dp"
android:layout_y="66dp"
android:text="WELCOME"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="178dp"
android:layout_y="238dp"
android:text="CARRY OUT" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="121dp"
android:layout_y="313dp"
android:text="DINE IN" />
// java code
package com.example.pizza;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickEventListener();
}
public void addButtonClickEventListener() {
// TODO Auto-generated method stub
Button button =(Button)findViewById(R.id.button1);
final Context context = MainActivity.this;
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent (context,RegActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.pizza;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class RegActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstancestate)
{
super.onCreate(savedInstancestate);
setContentView(R.layout.regfrm);
addButtonOnClickEventListener();
}
private void addButtonOnClickEventListener()
{
// TODO Auto-generated method stub
Button button =(Button)findViewById(R.id.button3);
final Context context = RegActivity.this;
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent (context,WelActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.pizza;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WelActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstancestate)
{
super.onCreate(savedInstancestate);
setContentView(R.layout.welcome);
addButtonOnClickEventListener();
}
private void addButtonOnClickEventListener() {
// TODO Auto-generated method stub
Button button =(Button)findViewById(R.id.button5);
final Context context = WelActivity.this;
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent (context,OrderActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pizza"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pizza.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.pizza.RegActivity"></activity>
<activity android:name="com.example.pizza.WelActivity"></activity>
</application>
</manifest>
02-12 19:21:05.430: I/Choreographer(820): Skipped 39 frames! The application may be doing too much work on its main thread.
02-12 19:21:05.930: D/gralloc_goldfish(820): Emulator without GPU emulation detected.
02-12 19:21:17.420: I/Choreographer(820): Skipped 61 frames! The application may be doing too much work on its main thread.
02-12 19:21:22.180: I/Choreographer(820): Skipped 128 frames! The application may be doing too much work on its main thread.
02-12 19:21:27.340: I/Choreographer(820): Skipped 35 frames! The application may be doing too much work on its main thread.
02-12 19:21:27.920: D/AndroidRuntime(820): Shutting down VM
02-12 19:21:27.920: W/dalvikvm(820): threadid=1: thread exiting with uncaught exception (group=0xb4a96b90)
02-12 19:21:28.020: E/AndroidRuntime(820): FATAL EXCEPTION: main
02-12 19:21:28.020: E/AndroidRuntime(820): Process: com.example.pizza, PID: 820
02-12 19:21:28.020: E/AndroidRuntime(820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pizza/com.example.pizza.WelActivity}: java.lang.NullPointerException
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.os.Looper.loop(Looper.java:137)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-12 19:21:28.020: E/AndroidRuntime(820): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 19:21:28.020: E/AndroidRuntime(820): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 19:21:28.020: E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-12 19:21:28.020: E/AndroidRuntime(820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-12 19:21:28.020: E/AndroidRuntime(820): at dalvik.system.NativeStart.main(Native Method)
02-12 19:21:28.020: E/AndroidRuntime(820): Caused by: java.lang.NullPointerException
02-12 19:21:28.020: E/AndroidRuntime(820): at com.example.pizza.WelActivity.addButtonOnClickEventListener(WelActivity.java:27)
02-12 19:21:28.020: E/AndroidRuntime(820): at com.example.pizza.WelActivity.onCreate(WelActivity.java:19)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.Activity.performCreate(Activity.java:5243)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 19:21:28.020: E/AndroidRuntime(820): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-12 19:21:28.020: E/AndroidRuntime(820): ... 11 more
02-12 19:21:32.050: I/Process(820): Sending signal. PID: 820 SIG: 9
02-12 19:21:34.340: I/Choreographer(837): Skipped 40 frames! The application may be doing too much work on its main thread.
02-12 19:21:34.430: D/gralloc_goldfish(837): Emulator without GPU emulation detected.
02-12 03:30:08.860: D/gralloc_goldfish(1119): Emulator without GPU emulation detected.
02-12 03:30:20.660: I/Choreographer(1119): Skipped 179 frames! The application may be doing too much work on its main thread.
02-12 03:30:22.400: I/Choreographer(1119): Skipped 32 frames! The application may be doing too much work on its main thread.
02-12 03:30:23.010: D/AndroidRuntime(1119): Shutting down VM
02-12 03:30:23.010: W/dalvikvm(1119): threadid=1: thread exiting with uncaught exception (group=0xb4a96b90)
02-12 03:30:23.100: E/AndroidRuntime(1119): FATAL EXCEPTION: main
02-12 03:30:23.100: E/AndroidRuntime(1119): Process: com.example.pizza, PID: 1119
02-12 03:30:23.100: E/AndroidRuntime(1119): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pizza/com.example.pizza.WelActivity}: java.lang.NullPointerException
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.os.Looper.loop(Looper.java:137)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-12 03:30:23.100: E/AndroidRuntime(1119): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 03:30:23.100: E/AndroidRuntime(1119): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 03:30:23.100: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-12 03:30:23.100: E/AndroidRuntime(1119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-12 03:30:23.100: E/AndroidRuntime(1119): at dalvik.system.NativeStart.main(Native Method)
02-12 03:30:23.100: E/AndroidRuntime(1119): Caused by: java.lang.NullPointerException
02-12 03:30:23.100: E/AndroidRuntime(1119): at com.example.pizza.WelActivity.addButtonOnClickEventListener(WelActivity.java:27)
02-12 03:30:23.100: E/AndroidRuntime(1119): at com.example.pizza.WelActivity.onCreate(WelActivity.java:19)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.Activity.performCreate(Activity.java:5243)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 03:30:23.100: E/AndroidRuntime(1119): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-12 03:30:23.100: E/AndroidRuntime(1119): ... 11 more
02-12 03:30:26.870: I/Process(1119): Sending signal. PID: 1119 SIG: 9
02-12 03:30:30.260: I/Choreographer(1144): Skipped 212 frames! The application may be doing too much work on its main thread.
02-12 03:30:30.380: D/gralloc_goldfish(1144): Emulator without GPU emulation detected.
答案 0 :(得分:2)
在第3个Activity布局中找不到名为button5
的ID。
Button button =(Button)findViewById(R.id.button5);
更改活动类中的ID。
答案 1 :(得分:0)
这是因为button5
id没有从其布局文件中获取。