查看我的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());
}
}
答案 0 :(得分:1)
private Exercisesetlist myExerciseSetList = new Exercisesetlist(this.getApplicationContext());
我不确定,但可能是因为没有调用OnCreate()方法,您将无法获得该Activity的上下文。所以尝试在OnCreate()方法中初始化这个,如下面所示,看看会发生什么。
private Exercisesetlist myExerciseSetList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.activity_exercisesetlist);
myExerciseSetList = new Exercisesetlist(this.getApplicationContext());
}
希望它会帮助你。
答案 1 :(得分:1)
在活动的Exercisesetlist myExerciseSetList
方法中初始化onCreate()
的实例,如下所示:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // TODO Auto-generated method stub setContentView(R.layout.activity_exercisesetlist); myExerciseSetList = new Exercisesetlist(this.getApplicationContext()); }
答案 2 :(得分:1)
我会试图回答这个问题。我假设,Exercisesetlist
是辅助类(setter / getter)。
改变这个:
private Exercisesetlist myExerciseSetList = new Exercisesetlist(this.getApplicationContext());
对此:
public class ExercisesetsActivity extends Activity {
private Exercisesetlist myExerciseSetList;
....
....
}
和
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.activity_exercisesetlist);
// This is temporary
copyDBifnotexists();
// INSTANTIATE THE Exercisesetlist INSTANCE
myExerciseSetList = new Exercisesetlist(ExercisesetsActivity.this);
ListView lv = (ListView) findViewById(R.id.lvExerciselist);
lv.setAdapter(new currentListAdapter());
}
答案 3 :(得分:0)
在您的清单文件中,在活动标记中指定了android:name as
android:name="com.myapp.activities.exercisesetsactivity.ExercisesetsActivity"
删除com.myapp并指定为
android:name=".activities.exercisesetsactivity.ExercisesetsActivity"
那么你就不会得到错误。