0

我有两个活动和一个数据库。这是第一个:

public class FeelingsMain extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initList();
        ListView lv = (ListView)findViewById(R.id.pageLayout);      
        //design the listview with the adapter
        SimpleAdapter simpleAdpt = new SimpleAdapter(this, feelingsList, android.R.layout.simple_list_item_1, new String[] {"feeling"},new int[] {android.R.id.text1});

        lv.setAdapter(simpleAdpt);

        lv.setOnItemClickListener(viewNeeds);   

    }

    List<Map<String, String>> feelingsList = new ArrayList<Map<String,String>>();

    private void initList(){
        //populate the feelingsList
        feelingsList.add(createFeeling("feeling","wonderful"));
        feelingsList.add(createFeeling("feeling","content"));
        feelingsList.add(createFeeling("feeling","joyful"));
        feelingsList.add(createFeeling("feeling","tired"));
        feelingsList.add(createFeeling("feeling","gay"));
        feelingsList.add(createFeeling("feeling","sad"));
        feelingsList.add(createFeeling("feeling","amazing"));


    }

    private HashMap<String, String>createFeeling(String key,String name){
    HashMap<String, String>feeling = new HashMap<String,String>();
    feeling.put(key, name);

    return feeling;

    }

    OnItemClickListener viewNeeds = new OnItemClickListener(){


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            //create variable for database input
            String feeling = feelingsList.get(arg2).toString();
            Bundle basket = new Bundle();
            basket.putString("aFeeling", feeling);


            Intent gotoNeeds=
                    new Intent(FeelingsMain.this, Needs.class);
            startActivity(gotoNeeds);
        }



    };



}

哪个应该开始这个活动......

public class Needs extends Activity {

    String gotTheFeeling;
    private DatabaseConnectToActivities database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Receive string from feelingsmain

        Bundle gotFeeling = getIntent().getExtras();
        gotTheFeeling = gotFeeling.getString("aFeeling");

        // new database object
        database = new DatabaseConnectToActivities(this);
        database.open();
        initList();

        ListView lv = (ListView) findViewById(R.id.pageLayout);

        SimpleAdapter simpleAdpt = new SimpleAdapter(this, needsList,
                android.R.layout.simple_list_item_1, new String[] { "need" },
                new int[] { android.R.id.text1 });

        lv.setAdapter(simpleAdpt);

        lv.setOnItemClickListener(backToFeelings);

    }

    List<Map<String, String>> needsList = new ArrayList<Map<String, String>>();
    protected String feelingForDb;

    public void initList() {

        needsList.add(createNeed("need", "space"));
        needsList.add(createNeed("need", "peace"));
        needsList.add(createNeed("need", "calm"));
        needsList.add(createNeed("need", "understanding"));
        needsList.add(createNeed("need", "to be heard"));
        needsList.add(createNeed("need", "to be seen"));
        needsList.add(createNeed("need", "love"));

    }

    private HashMap<String, String> createNeed(String key, String name) {
        HashMap<String, String> need = new HashMap<String, String>();
        need.put(key, name);

        return need;

    }

    OnItemClickListener backToFeelings = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            String need = needsList.get(arg2).toString();
            database.insertEmotions(gotTheFeeling, need);
            database.close();
            Intent toFeelings = new Intent(Needs.this, FeelingsMain.class);
            startActivity(toFeelings);

        }

    };

}

但是当我单击一个列表视图项时,我得到一个 NullPointerException。这是日志猫:

07-22 21:01:54.174: E/AndroidRuntime(9687): FATAL EXCEPTION: main
07-22 21:01:54.174: E/AndroidRuntime(9687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.feeling/com.example.feeling.Needs}: java.lang.NullPointerException
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.os.Looper.loop(Looper.java:137)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at dalvik.system.NativeStart.main(Native Method)
07-22 21:01:54.174: E/AndroidRuntime(9687): Caused by: java.lang.NullPointerException
07-22 21:01:54.174: E/AndroidRuntime(9687):     at com.example.feeling.Needs.onCreate(Needs.java:31)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.Activity.performCreate(Activity.java:5104)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 21:01:54.174: E/AndroidRuntime(9687):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-22 21:01:54.174: E/AndroidRuntime(9687):     ... 11 more
07-22 21:01:55.531: D/dalvikvm(9687): GC_CONCURRENT freed 129K, 10% free 2648K/2916K, paused 107ms+57ms, total 1409ms
07-22 21:06:55.315: I/Process(9687): Sending signal. PID: 9687 SIG: 9

这也是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.feeling"
    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.feeling.FeelingsMain"
            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.feeling.Needs"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.feeling.DatabaseHelp"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.example.feeling.DatabaseConnectToActivities"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

你能帮助确定这个异常的原因吗?

4

2 回答 2

1

你没有在你的意图中添加任何额外内容。

Bundle gotFeeling = getIntent().getExtras();
gotTheFeeling = gotFeeling.getString("aFeeling");

但你试图得到一个。

String feeling = feelingsList.get(arg2).toString();
Bundle basket = new Bundle();
basket.putString("aFeeling", feeling);


Intent gotoNeeds=
        new Intent(FeelingsMain.this, Needs.class);
startActivity(gotoNeeds);

我相信您已将您的购物篮捆绑与您尝试发送的附加内容混淆了。

试试这个。

String feeling = feelingsList.get(arg2).toString();
Intent gotoNeeds=
        new Intent(FeelingsMain.this, Needs.class);

gotoNeeds.putExtra("aFeeling", feeling);
startActivity(gotoNeeds);
于 2013-07-23T14:04:56.873 回答
0

尝试

gotoNeeds.putExtra("aFeeling", feeling);

而不是创建新的Bundle。

于 2013-07-23T14:11:44.860 回答