0

看看我的 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,它工作正常。但是,我希望首先出现练习集活动。

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());
}
}
4

4 回答 4

1
private Exercisesetlist myExerciseSetList = new Exercisesetlist(this.getApplicationContext());

我不确定,但这可能是因为如果不调用 OnCreate() 方法,您将无法获得该活动的上下文。所以尝试在 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());

}

希望它会帮助你。

于 2013-01-23T05:02:00.800 回答
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());
      }
于 2013-01-23T05:04:28.717 回答
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());
}
于 2013-01-23T05:04:41.540 回答
0

在您的清单文件中,在活动标记中,您将 android:name 指定为

        android:name="com.myapp.activities.exercisesetsactivity.ExercisesetsActivity"

删除 com.myapp 并指定为

      android:name=".activities.exercisesetsactivity.ExercisesetsActivity"

那么您将不会收到错误消息。

于 2013-01-23T06:31:06.197 回答