只要我没有将“Startup”活动作为第一个活动,我的代码就可以运行。当我的主要活动是第一个并且通过所有工作功能完成整个应用程序时,它可以工作。我必须缺少一些东西才能使这个页面成为应用程序启动时的页面。
logcat的
10-30 20:30:22.137: D/AndroidRuntime(783): Shutting down VM
10-30 20:30:22.137: W/dalvikvm(783): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-30 20:30:22.147: E/AndroidRuntime(783): FATAL EXCEPTION: main
10-30 20:30:22.147: E/AndroidRuntime(783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidassignment2/com.example.androidassignment2.Startup}: java.lang.NullPointerException
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.os.Looper.loop(Looper.java:137)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-30 20:30:22.147: E/AndroidRuntime(783): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 20:30:22.147: E/AndroidRuntime(783): at java.lang.reflect.Method.invoke(Method.java:525)
10-30 20:30:22.147: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-30 20:30:22.147: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 20:30:22.147: E/AndroidRuntime(783): at dalvik.system.NativeStart.main(Native Method)
10-30 20:30:22.147: E/AndroidRuntime(783): Caused by: java.lang.NullPointerException
10-30 20:30:22.147: E/AndroidRuntime(783): at com.example.androidassignment2.Startup.onCreate(Startup.java:19)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.Activity.performCreate(Activity.java:5133)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-30 20:30:22.147: E/AndroidRuntime(783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-30 20:30:22.147: E/AndroidRuntime(783): ... 11 more
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/Hello"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:text="@string/Hello" />
<Button android:id="@+id/QuitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_quit" />
</LinearLayout>
代码
package com.example.androidassignment2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Startup extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
Button next = (Button) findViewById(R.id.QButton);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
@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;
}
}
最后是manifest.xml文件。怀疑可能存在问题。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidassignment2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.androidassignment2.Startup"
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.androidassignment2.MainActivity"
android:label="@string/title_activity_android_assignment2_1" >
</activity>
<activity
android:name="com.example.androidassignment2.AndroidAssignment2_1"
android:label="@string/title_activity_android_assignment2_1">
</activity>
<activity
android:name="com.example.androidassignment2.AndroidAssignment2_2"
android:label="@string/title_activity_android_assignment2_1">
</activity>
<activity
android:name="com.example.androidassignment2.AndroidAssignment2_3"
android:label="@string/title_activity_android_assignment2_1">
</activity>
<activity
android:name="com.example.androidassignment2.AndroidAssignment2_4"
android:label="@string/title_activity_android_assignment2_1">
</activity>
<activity
android:name="com.example.androidassignment2.AndroidAssignment2_5"
android:label="@string/title_activity_android_assignment2_1">
</activity>
<activity
android:name="com.example.androidassignment2.Goodbye"
android:label="@string/title_activity_android_assignment2_1">
</activity>
</application>
</manifest>
答案 0 :(得分:3)
您的按钮ID错误。您的xml中未定义QButton
。
而不是
Button next = (Button) findViewById(R.id.QButton);
使用
Button next = (Button) findViewById(R.id.QuitButton);