0

私の問題は listview.adapter(adapter) にあり、次のエラーログが返されます:

09-27 12:30:02.550: E/AndroidRuntime(2151): FATAL EXCEPTION: main
09-27 12:30:02.550: E/AndroidRuntime(2151): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prueba_listas/com.example.prueba_listas.Lists}: java.lang.NullPointerException
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.os.Looper.loop(Looper.java:137)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at java.lang.reflect.Method.invoke(Method.java:525)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at dalvik.system.NativeStart.main(Native Method)
09-27 12:30:02.550: E/AndroidRuntime(2151): Caused by: java.lang.NullPointerException
09-27 12:30:02.550: E/AndroidRuntime(2151):     at com.example.prueba_listas.Lists.onCreate(Lists.java:43)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.Activity.performCreate(Activity.java:5133)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-27 12:30:02.550: E/AndroidRuntime(2151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-27 12:30:02.550: E/AndroidRuntime(2151):     ... 11 more

コード:

import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Lists extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView listview = (ListView) findViewById(R.layout.listview);

        // String[] array = getResources().getStringArray(R.array.lista);
        String[] array = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
                "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
                "Android", "iPhone", "WindowsMobile" };
        final ArrayList<String> lista = new ArrayList<String>();
        for (int i = 0; i < array.length; ++i) {
            lista.add(array[i]);
        }

        Log.i("OnCreate", "Listview adpt ");

        final StableArrayAdapter adapter = new StableArrayAdapter(
                getApplicationContext(), android.R.layout.simple_list_item_1,
                (List<String>) lista);
        for (int i = 0; i < lista.size(); i++)
            Log.i("OnCreate", adapter.getItem(i));

        Log.i("OnCreateadaptercount", String.valueOf(adapter.getCount()));

                 //here is the problem!
            listview.setAdapter(adapter);

//      listview.getAdapter();
        Log.i("OnCreate", "set adpater final ");

    }
}
4

1 に答える 1