0

我正在制作一个列表视图,我希望它显示字符串数组中的内容。这是我的扫描仪课程:

package com.rezoluz.planesim;

import java.util.ArrayList;

public class Scanner {

    public ArrayList<String> gameList;
    public String[] gameListFin;

    public void getFiles() {
        gameList.add("testo7r");
        gameList.add("test5or");
        gameList.add("te4stor");
        gameList.add("tes3tor");
        gameList.add("testo2r");
        gameListFin = gameList.toArray(new String[gameList.size()]);
    }

}

这是我的 ShortcutListActivty 类:

package com.rezoluz.planesim;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ShortcutListActivity extends Activity {

    Scanner scanner = new Scanner();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shortcuts_list);

        scanner.getFiles();
        ListView listView = (ListView)findViewById(R.id.shortcutsList);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, scanner.gameListFin));
    }

}

但是当我打开 ShortcutsListActivity 时,它会强制关闭,这里是 logcat。

04-20 15:29:24.343: D/AndroidRuntime(17039): Shutting down VM 04-20
 15:29:24.343: W/dalvikvm(17039): threadid=1: thread exiting with
 uncaught exception (group=0x4001d7e8) 04-20 15:29:24.478:
 E/AndroidRuntime(17039): FATAL EXCEPTION: main 04-20 15:29:24.478:
 E/AndroidRuntime(17039): java.lang.RuntimeException: Unable to start
 activity
 ComponentInfo{com.rezoluz.planesim/com.rezoluz.planesim.ShortcutListActivity}:
 java.lang.NullPointerException 04-20 15:29:24.478:
 E/AndroidRuntime(17039):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 android.app.ActivityThread.access$2300(ActivityThread.java:125) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 android.os.Handler.dispatchMessage(Handler.java:99) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 android.os.Looper.loop(Looper.java:123) 04-20 15:29:24.478:
 E/AndroidRuntime(17039):   at
 android.app.ActivityThread.main(ActivityThread.java:4627) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 java.lang.reflect.Method.invokeNative(Native Method) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 java.lang.reflect.Method.invoke(Method.java:521) 04-20 15:29:24.478:
 E/AndroidRuntime(17039):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 dalvik.system.NativeStart.main(Native Method) 04-20 15:29:24.478:
 E/AndroidRuntime(17039): Caused by: java.lang.NullPointerException
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 com.rezoluz.planesim.Scanner.getFiles(Scanner.java:11) 04-20
 15:29:24.478: E/AndroidRuntime(17039):     at
 com.rezoluz.planesim.ShortcutListActivity.onCreate(ShortcutListActivity.java:19)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 04-20 15:29:24.478: E/AndroidRuntime(17039):   ... 11 more

我可以看到它引用了这个:gameList.add("testo7r");还有这个和这个,scanner.getFiles();但我没有看到那里的问题,请帮忙!感谢您的时间和帮助,zeokila。

4

1 回答 1

3

你忘了初始化..所以它必须是一个空指针异常......

public class Scanner {

    public ArrayList<String> gameList = new ArrayList<String>();
    public String[] gameListFin;

    public void getFiles() {
        gameList.add("testo7r");
        gameList.add("test5or");
        gameList.add("te4stor");
        gameList.add("tes3tor");
        gameList.add("testo2r");
        gameListFin = gameList.toArray(new String[gameList.size()]);
    }

}
于 2012-04-20T13:36:47.230 回答