0

我已经在服务器上的表中存储了一些数据,现在我试图在 ListView 中显示这些数据,但是每次我得到NULL POINTER EXCEPTION请检查​​我下面的代码

OrdersAdapter.java:

  public class OrdersAdapter extends BaseAdapter {
TextView tName,tId,tOid ;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
String strName,strMemberID ;

public OrdersAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listrow_orders, null);

    tId = (TextView)vi.findViewById(R.id.txtTotalAmount);
    tName = (TextView)vi.findViewById(R.id.txtItemDetails);

    HashMap<String, String> item = new HashMap<String, String>();
    item = data.get(position);

    tId.setText(item.get(strName));
    tName.setText(item.get(strMemberID));


        String url = "http://172.16.0.4/res/order_fetch.php";
        Intent intent= activity.getIntent();
        String MemberID = intent.getStringExtra("MemberID");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sMemberID", MemberID));
        String resultServer  = getHttpPost(url,params);

        strMemberID = "";
        strName = "";


        JSONObject c;
        try {
        c = new JSONObject(resultServer);
        strMemberID = c.getString("TotalAmount");               
        strName = c.getString("ItemDetails");

        if(!strMemberID.equals(""))
        {                   
            tName.setText(strName);
            tId.setText(strMemberID);               
        }
        else
        {               
            tName.setText("-");
            tId.setText("-");
        }
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        return vi;

                  }     

    public String getHttpPost(String url,List<NameValuePair> params) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = client.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Status OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download result..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}
   }

OrdersActivity.java:

   public class OrdersActivity extends Activity 
{ 
    public static final String LOG_TAG = "OrdersActivity";
    ArrayList<HashMap<String, String>> d;
    ListView mLstView1;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);

        mLstView1 = (ListView) findViewById(R.id.listView1);
        OrdersAdapter ordersAdapter = new OrdersAdapter(OrdersActivity.this,d);            
        mLstView1.setAdapter(ordersAdapter);


        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    }
}

日志报告:

     05-22 14:50:29.373: E/AndroidRuntime(780): FATAL EXCEPTION: main
05-22 14:50:29.373: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{OrdersActivity}: java.lang.NullPointerException
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.os.Looper.loop(Looper.java:137)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-22 14:50:29.373: E/AndroidRuntime(780):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 14:50:29.373: E/AndroidRuntime(780):  at java.lang.reflect.Method.invoke(Method.java:511)
05-22 14:50:29.373: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-22 14:50:29.373: E/AndroidRuntime(780):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-22 14:50:29.373: E/AndroidRuntime(780):  at dalvik.system.NativeStart.main(Native Method)
05-22 14:50:29.373: E/AndroidRuntime(780): Caused by: java.lang.NullPointerException
05-22 14:50:29.373: E/AndroidRuntime(780):  at OrdersAdapter.getCount(OrdersAdapter.java:58)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.widget.ListView.setAdapter(ListView.java:462)
05-22 14:50:29.373: E/AndroidRuntime(780):  at OrdersActivity.onCreate(OrdersActivity.java:30)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.Activity.performCreate(Activity.java:5104)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-22 14:50:29.373: E/AndroidRuntime(780):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-22 14:50:29.373: E/AndroidRuntime(780):  ... 11 more
4

2 回答 2

1

在您的OrdersActivity, 您从未初始化d( HashMap) 您作为参数传递给 Adpater。

public int getCount() {
    return (data == null) ? 0 : data.size();
}
于 2013-05-22T09:30:19.763 回答
0

你的异常发生在这里。

at OrdersAdapter.getCount(OrdersAdapter.java:58)
于 2013-05-22T09:32:35.113 回答