私の AndroidMenifest.xml を見てください
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.myapp.activities.exercisesetsactivity.ExercisesetsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.myapp.activities.mainactivity.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>
<service
android:name="com.myapp.services.guideservice.GuideService"
android:exported="false" >
</service>
</application>
</manifest>
最初は、MainActivity がデフォルトのアクティビティでした。後で、ExercisesetsActivity に変更しようとしました。それが、logcat で以下のエラーが発生し始めた場所です。
>
01-22 22:09:51.831: E/AndroidRuntime(22676): FATAL EXCEPTION: main
01-22 22:09:51.831: E/AndroidRuntime(22676): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myapp/com.myapp.activities.exercisesetsactivity.ExercisesetsActivity}: java.lang.NullPointerException
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.os.Looper.loop(Looper.java:137)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-22 22:09:51.831: E/AndroidRuntime(22676): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 22:09:51.831: E/AndroidRuntime(22676): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 22:09:51.831: E/AndroidRuntime(22676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-22 22:09:51.831: E/AndroidRuntime(22676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-22 22:09:51.831: E/AndroidRuntime(22676): at dalvik.system.NativeStart.main(Native Method)
01-22 22:09:51.831: E/AndroidRuntime(22676): Caused by: java.lang.NullPointerException
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
01-22 22:09:51.831: E/AndroidRuntime(22676): at com.myapp.activities.exercisesetsactivity.ExercisesetsActivity.<init>(ExercisesetsActivity.java:24)
01-22 22:09:51.831: E/AndroidRuntime(22676): at java.lang.Class.newInstanceImpl(Native Method)
01-22 22:09:51.831: E/AndroidRuntime(22676): at java.lang.Class.newInstance(Class.java:1319)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
01-22 22:09:51.831: E/AndroidRuntime(22676): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
01-22 22:09:51.831: E/AndroidRuntime(22676): ... 11 more
MainActivity に変更すると、正常に動作します。ただし、ExercisesetsActivity を最初に表示する必要があります。
package com.myapp.activities.exercisesetsactivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.myapp.R;
import com.myapp.activities.mainactivity.MainActivity;
import com.myapp.businessclasses.Exercisesetlist;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ExercisesetsActivity extends Activity {
**private Exercisesetlist myExerciseSetList = new Exercisesetlist(this.getApplicationContext());**
private class currentListAdapter extends ArrayAdapter<String> {
public currentListAdapter() {
super(ExercisesetsActivity.this, R.layout.activity_exercisesetlist,
ExercisesetsActivity.this.myExerciseSetList.setNames);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.activity_exercisesetlist, parent,
false);
TextView label = (TextView) row.findViewById(R.id.tvItems);
String strContent = ExercisesetsActivity.this.myExerciseSetList.setNames
.get(position);
label.setText(strContent);
label.setTag(position);
label.setOnClickListener(exerciseClickListner);
return row;
}
}
private android.view.View.OnClickListener exerciseClickListner = new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int pos = (Integer) v.getTag();
Intent intent = new Intent(ExercisesetsActivity.this.getApplicationContext(),
MainActivity.class);
intent.putExtra(ExercisesetsActivity.this.getResources().getString(R.string.EXERCISESETID),
ExercisesetsActivity.this.myExerciseSetList.setIDs.get(pos));
startActivity(intent);
}
};
// This is temporary
public void copyDBifnotexists() {
String DATABASE_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/myapp/data/";
String DATABSE_NAME = "db.db";
File folders = new File(DATABASE_PATH);
File file = new File(folders, DATABSE_NAME);
if (!file.exists()) {
folders.mkdirs();
try {
InputStream inputStream = this.getResources().openRawResource(
R.raw.db);
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.close();
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.activity_exercisesetlist);
// This is temporary
copyDBifnotexists();
ListView lv = (ListView) findViewById(R.id.lvExerciselist);
lv.setAdapter(new currentListAdapter());
}
}