私のコードは、最初のアクティビティとして「スタートアップ」アクティビティがない場合はいつでも機能します。私のメインアクティビティが最初のものであり、すべての機能を備えたアプリ全体を通過するときに機能します。このページをアプリケーションの開始時に機能させるには、何かが欠けている必要があります。
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>