0

私は現在、カスタムを含むアプリケーションを開発していますListView。カスタムアレイアダプタを開発しました。私のアプリはここでクラッシュすると思います:

ListView DirectoryView = (ListView) findViewById(R.id.fileListView);

したがって、エラーはactivity_main.xmlにあると思います。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
    android:id="@+id/fileListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
</ListView>

これが私のLogCatです:

09-09 11:19:21.254: E/Trace(1152): error opening trace file: No such file or directory (2)
09-09 11:19:21.484: D/AndroidRuntime(1152): Shutting down VM
09-09 11:19:21.484: W/dalvikvm(1152): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-09 11:19:21.504: E/AndroidRuntime(1152): FATAL EXCEPTION: main
09-09 11:19:21.504: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamdroid.explorer/com.teamdroid.explorer.MainActivity}: java.lang.NullPointerException
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.os.Looper.loop(Looper.java:137)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at java.lang.reflect.Method.invoke(Method.java:511)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at dalvik.system.NativeStart.main(Native Method)
09-09 11:19:21.504: E/AndroidRuntime(1152): Caused by: java.lang.NullPointerException
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.Activity.findViewById(Activity.java:1825)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at com.teamdroid.explorer.listDirectory.getDirectory(listDirectory.java:20)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at com.teamdroid.explorer.MainActivity.onCreate(MainActivity.java:33)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.Activity.performCreate(Activity.java:5008)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 11:19:21.504: E/AndroidRuntime(1152):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 11:19:21.504: E/AndroidRuntime(1152):     ... 11 more

助けてくれますか。このエラーを2日間検索しています。ありがとう!

編集: 私はこれにディレクトリをリストする3番目のファイルを持っていますlistview

    public class listDirectory extends ListActivity {

    String[] DirList;

    public void onCreate(Bundle icicle) {
        setContentView(R.layout.activity_main);
        super.onCreate(icicle);
    }   

    public void getDirectory (){
        MainActivity main = new MainActivity();
        String path = main.getPath();

        ListView DirectoryView = (ListView) findViewById(R.id.fileListView);
        CustomArrayAdapter adapter = new CustomArrayAdapter(getApplicationContext(), DirList);

        File file = new File(path);
        File[] FileList = file.listFiles();

        java.util.Arrays.sort(FileList);

        for(int i = 0; i < FileList.length; i++){
            if(FileList[i].isDirectory()){
                DirList[i] = (FileList[i].getName() + " [folder]");
            } else{
                DirList[i] = (FileList[i].getName() + " [file]");
            }
         }

         DirectoryView.setAdapter(adapter);
    }
}

このファイルにはエラーがあると思います。

4

2 に答える 2

1

onCreate() メソッド内でコンテンツ ビューを設定していないため、findViewById はビュー オブジェクトを見つけることができず、null を返していると思います。

XML レイアウト ファイルが my_layout.xml であるとします。onCreate(...) 内に次の行を追加します。

setContentView(R.layout.my_layout);
于 2012-09-09T09:40:48.103 に答える
0

非常に紛らわしいコード...

MainActivityの新しいインスタンスだと思いますがlistDirectory、これは呼び出されないためlistDirectory.onCreate()、コンテンツ ビューは null です。そのため、findViewById()内部listDirectory.getDirectory()では、null であり NPE を引き起こす MainActivity のビューではなく、listDirectory のコンテンツ ビューでビューが検索されます。

于 2016-10-14T09:34:27.920 に答える