0

创建多列列表视图时出现以下错误在此之前在 main.xml 中解决了 nullpointer 异常之后此错误。

Error 
    12-13 19:04:00.524: E/AndroidRuntime(1588): FATAL EXCEPTION: main
        12-13 19:04:00.524: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysampleapp/com.example.mysampleapp.LabelUser}: java.lang.NullPointerException
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.os.Handler.dispatchMessage(Handler.java:99)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.os.Looper.loop(Looper.java:123)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread.main(ActivityThread.java:4627)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at java.lang.reflect.Method.invokeNative(Native Method)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at java.lang.reflect.Method.invoke(Method.java:521)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at dalvik.system.NativeStart.main(Native Method)
        12-13 19:04:00.524: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at com.example.mysampleapp.LabelUser.onCreate(LabelUser.java:70)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
        12-13 19:04:00.524: E/AndroidRuntime(1588):     ... 11 more

我为双列列表视图编写的代码。

导入 java.util.ArrayList;导入 java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class LabelUser extends ListActivity {
    String[] labelArray;
    String[] labelIdArray; 
    String[] labelRateArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.labeluser);
        ControlDashboard obj = new ControlDashboard();
        String url = "http://192.168.1.73:3000/labels/show_label_android/?user="+Constants.ID;
        JSONObject jobj = obj.getJSONFromUrl(url);

        try {
            JSONObject array = jobj.getJSONObject("list"); 
            JSONArray menuitemArr = array.getJSONArray("array");
            labelArray = new String[menuitemArr.length()];
            labelIdArray = new String[menuitemArr.length()];
            labelRateArray = new String[menuitemArr.length()];
             System.out.println(menuitemArr.length());
            for (int i = 0; i < menuitemArr.length(); i++) { 
                // printing the values to the logcat 
               labelRateArray[i] =  menuitemArr.getJSONObject(i).getString("current_royalty_rate").toString(); 
               labelIdArray[i] = menuitemArr.getJSONObject(i).getString("id").toString(); 
               labelArray[i] = menuitemArr.getJSONObject(i).getString("name").toString();       
               System.out.println(labelArray[i]);
            } 
            System.out.println();
            ListView list = (ListView) findViewById(R.id.listView1);
            ArrayList<HashMap<String, String>> mylistData = new ArrayList<HashMap<String, String>>();

            String[] columnTags = new String[] {"col1", "col2"};
            int[] columnIds = new int[] {R.id.column1, R.id.column2};
            for(int i=0; i<2; i++)
            {
             HashMap<String,String> map = new HashMap<String, String>();
             //initialize row data
             for(int j=0; j<3; j++)
             {

                map.put(columnTags[i], labelArray[j]);
                map.put(columnTags[i], labelRateArray[j]);

             }
             mylistData.add(map);
            }
            SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.two_col_row,columnTags , columnIds);
list.setAdapter(arrayAdapter);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
    //@Override
     // protected void onListItemClick(ListView l, View v, int position, long id) {
    //    String item = (String) getListAdapter().getItem(position);
    //    Constants.LABELID = labelIdArray[(int) id];
    //    startActivity(new Intent("com.example.mysampleapp.DASHBOARDTAB"));


    //  }


}
4

1 回答 1

2

您在没有 setContentView 的情况下调用 findViewbyId

   ListView list = (ListView) findViewById(R.id.listView1);

所以我猜 list 是 null 。所以

list.setAdapter(arrayAdapter);

给出 NullPointerException

您应该取消注释 setContentView 并确保您的 xml 有一个名为 listView1 的 listView

于 2012-12-13T13:48:13.360 回答