实现一个简单的ListView

时间:2014-06-01 21:12:21

标签: android android-listview

尝试运行使用ListView和ArrayAdapter的应用程序时出错。我正在按照Android指南在此处显示的基本流程(http://developer.android.com/guide/topics/ui/declaring-layout.html),但似乎存在问题。知道问题是什么吗?

主要活动

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

        String[] myArr = new String[] {"Here", "is", "some", "test", "data"};

        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArr);   

        ListView view = (ListView) findViewById(R.id.listView);
        view.setAdapter(adapter);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }




}

活动主要

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.firstapp.MainActivity"
    tools:ignore="MergeRootFrame" />

Fragment Main

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

    <ListView android:id="@+id/listView" 
              android:layout_height="match_parent"
              android:layout_width="match_parent"/>

</RelativeLayout>

logcat的

06-01 14:17:37.770: D/AndroidRuntime(32461): Shutting down VM
06-01 14:17:37.770: W/dalvikvm(32461): threadid=1: thread exiting with uncaught exception (group=0x4177b898)
06-01 14:17:37.770: E/AndroidRuntime(32461): FATAL EXCEPTION: main
06-01 14:17:37.770: E/AndroidRuntime(32461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstapp/com.example.firstapp.MainActivity}: java.lang.NullPointerException
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.os.Looper.loop(Looper.java:137)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread.main(ActivityThread.java:5419)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at java.lang.reflect.Method.invokeNative(Native Method)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at java.lang.reflect.Method.invoke(Method.java:525)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at dalvik.system.NativeStart.main(Native Method)
06-01 14:17:37.770: E/AndroidRuntime(32461): Caused by: java.lang.NullPointerException
06-01 14:17:37.770: E/AndroidRuntime(32461):    at com.example.firstapp.MainActivity.onCreate(MainActivity.java:28)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.Activity.performCreate(Activity.java:5372)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-01 14:17:37.770: E/AndroidRuntime(32461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
06-01 14:17:37.770: E/AndroidRuntime(32461):    ... 11 more

2 个答案:

答案 0 :(得分:2)

您的活动正在使用此布局:R.layout.activity_main

此布局不包含ListView。 所以这条线产生了一个NPE。

ListView view = (ListView) findViewById(R.id.listView);
view.setAdapter(adapter); //view is null!!

您应该更改您的代码。 如果您想使用片段,则应在片段内设置列表和适配器。 否则,请删除片段调用,并在activity_main布局中移动List。

答案 1 :(得分:1)

问题是ListView对象是空的,因为您试图在ListView中找到Activity,但实际上是ListView 对象在Fragment中,您在logcat中的错误是nullPointException 所以只需简单地将代码移至FragmentonCreateView回调,这样您就可以在那里定义ListView并设置adapter

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
           String[] myArr = new String[] {"Here", "is", "some", "test", "data"};

    ListAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, myArr);   

    ListView view = (ListView) rootView.findViewById(R.id.listView);
    view.setAdapter(adapter);
    return rootView;
}