0

我正在编写一个应用程序,我正在尝试从服务器 PHPMYADMIN 获取以前的订单详细信息,并希望在 ListView 中显示这些订单详细信息,但是每当我运行我的应用程序得到 NullPointerException 时,请检查我的代码并告诉我我缺少什么和我在哪里做错了..

日志说:

05-22 12:33:35.652: E/AndroidRuntime(786): Caused by: java.lang.NullPointerException
05-22 12:33:35.652: E/AndroidRuntime(786):  at OrdersActivity.showInfo(OrdersActivity.java:121)
05-22 12:33:35.652: E/AndroidRuntime(786):  at OrdersActivity.onCreate(OrdersActivity.java:75)
05-22 12:33:35.652: E/AndroidRuntime(786):  at android.app.Activity.performCreate(Activity.java:5104)
05-22 12:33:35.652: E/AndroidRuntime(786):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-22 12:33:35.652: E/AndroidRuntime(786):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-22 12:33:35.652: E/AndroidRuntime(786):  ... 11 more

错误行号 75 是:showinfo();

错误行号 121 是:if(!strMemberID.equals(""))

OrdersActivity.java:

public class OrdersActivity extends Activity {

TextView total, items ;
    String strMemberID,resultServer,MemberID;   

    ListView list;
    OrdersAdapter adapter;
    ArrayList<HashMap<String, String>> itemsList;


    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);

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

        itemsList = new ArrayList<HashMap<String, String>>();
        list = (ListView) findViewById(R.id.listView1);
        adapter = new OrdersAdapter(this, itemsList);
        list.setAdapter(adapter);



        if (isNetworkAvailable()) {
            new MyAsyncTask().execute();
        } else {

            AlertDialog alertDialog = new AlertDialog.Builder(OrdersActivity.this).create();
            alertDialog.setMessage("The Internet connection appears to be offline.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alertDialog.show();
        }
    }

    public void showInfo()
    {
            total = (TextView)findViewById(R.id.txtTotalAmount);
            items = (TextView)findViewById(R.id.txtItemDetails);

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

            String strTotal = "";
            String strItems = "";


            JSONObject c;
            try {
            c = new JSONObject(resultServer);
            strTotal = c.getString("TotalAmount");              
            strItems = c.getString("ItemDetails");

            if(!strMemberID.equals(""))
            {                   
                total.setText(strTotal);
                items.setText(strItems);                
            }
            else
            {               
                total.setText("-");
                items.setText("-");
            }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       
    }       

        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();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return (info != null);
    }

    class MyAsyncTask extends
            AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
        private ProgressDialog progressDialog = new ProgressDialog(
                OrdersActivity.this);

        @Override
        protected void onPreExecute() {
            progressDialog.setMessage("Loading, Please wait.....");
            progressDialog.show();
        }

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... params) {
            // TODO Auto-generated method stub
            return null;
        }


        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            list = (ListView) findViewById(R.id.listView1);
            adapter = new OrdersAdapter(OrdersActivity.this, itemsList);
            list.setAdapter(adapter);

            this.progressDialog.dismiss();
            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                }

            });

        }
    }
}

OrdersAdapter.java:

public class OrdersAdapter extends BaseAdapter {

    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);

        TextView title = (TextView)vi.findViewById(R.id.txtTotalAmount); 
        TextView description = (TextView)vi.findViewById(R.id.txtItemDetails); 


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

        title.setText(item.get(strName));
        description.setText(strMemberID);
        return vi;
    }
}
4

2 回答 2

1

声明你strMemberIDString strMemberID = "";

NullPointorException将解决... :)

于 2013-05-22T07:16:05.403 回答
0

String在 Class 范围内声明该类型时,该类型的默认初始化为null。这意味着你不能在初始化它之前使用它。您可以通过以下方式更改 if:

if(strMemberID != null && !strMemberID.equals("")) {
 }
于 2013-05-22T07:27:29.840 回答