-1

我想将 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>

我可以以其他方式添加它吗?

干杯

编辑:

这是一个日志猫

    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
4

1 回答 1

0

As per your code (snippet below), it seems that activity_main has a pager component in it. However, the layout file that you have shared does not have it. Now, since you are looking for R.id.listview in same layout file as that contained R.id.pager, obviously, there is something wrong.

So, I am assuming that your expectation that there is a component R.id.listview present in layout is wrong, and hence you are getting null pointer exception

setContentView(R.layout.activity_main);

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

....
ListView listView = ( ListView ) findViewById(R.id.listview);  //this is null
于 2013-07-07T14:52:04.030 回答