添加适配器时,应用已停止

时间:2013-07-07 14:07:13

标签: java android android-listview

我想将ListView添加到我的项目中(现在我有一个TextView和一个进度条)。 当我用listView添加适配器时,我的应用程序停止了(不幸的是应用程序停止了):/

我在MainActivity类中的onCreate方法。

super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_main);

    //Pager settings
    ViewPager pager = (ViewPager)findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
    pager.setAdapter(pagerAdapter);

    //Settings of progress bar
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
    mProgressDialog.setMessage("Work in Progress ...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Anuluj", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            status = false;
            dialog.dismiss();
            dialog.cancel();
        }
    });
    //===========================================
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        
    int s=0;

    for(int i=0;i<10;i++){
        s++;
        if(s==3){
            s=0;
        }
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", "Item" + i);  
        hm.put("flag", Integer.toString(flags[s]));            
        aList.add(hm);        
    }

    String[] from = { "flag","txt","cur" };
    int[] to = { R.id.flag,R.id.txt,R.id.cur};        
    SimpleAdapter adapter  = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
    ListView listView = ( ListView ) findViewById(R.id.listview); 
    listView.setAdapter(adapter); <=== here is a problem ( i think).

}

这是一个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aaa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:duplicateParentState="true"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/in"
        android:textColor="@drawable/text_color" />




        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
    />

</LinearLayout>

和listview_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView 
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/hello"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"        

        />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"            
        >    

        <TextView 
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"             
        />          

        <TextView 
            android:id="@+id/cur"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"        
        />

    </LinearLayout>

我可以用不同的方式添加它吗?

干杯

编辑:

这是一个logcat

    07-07 14:17:36.373: E/AndroidRuntime(759): FATAL EXCEPTION: main
07-07 14:17:36.373: E/AndroidRuntime(759): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.viewpagerdemo/in.sirocco.exercise.MainActivity}: java.lang.NullPointerException
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.os.Looper.loop(Looper.java:137)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-07 14:17:36.373: E/AndroidRuntime(759):  at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:17:36.373: E/AndroidRuntime(759):  at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:17:36.373: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-07 14:17:36.373: E/AndroidRuntime(759):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-07 14:17:36.373: E/AndroidRuntime(759):  at dalvik.system.NativeStart.main(Native Method)
07-07 14:17:36.373: E/AndroidRuntime(759): Caused by: java.lang.NullPointerException
07-07 14:17:36.373: E/AndroidRuntime(759):  at in.sirocco.exercise.MainActivity.onCreate(MainActivity.java:86)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.Activity.performCreate(Activity.java:5104)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-07 14:17:36.373: E/AndroidRuntime(759):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-07 14:17:36.373: E/AndroidRuntime(759):  ... 11 more

1 个答案:

答案 0 :(得分:0)

根据您的代码(下面的代码段),似乎activity_main中有一个寻呼机组件。但是,您共享的布局文件没有它。现在,因为你在与包含R.id.pager的布局文件相同的布局文件中寻找R.id.listview,显然有一些错误。

所以,我假设您期望布局中存在组件R.id.listview是错误的,因此您将获得空指针异常

setContentView(R.layout.activity_main);

//Pager settings
ViewPager pager = (ViewPager)findViewById(R.id.pager);

....
ListView listView = ( ListView ) findViewById(R.id.listview);  //this is null