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

strMemberIDあなたを宣言するString strMemberID = "";

NullPointorException解決されます... :)

于 2013-05-22T07:16:05.403 に答える
0

StringClass スコープで宣言されている場合、型の既定の初期化は null です。これは、初期化する前に使用できないことを意味します。この方法で if を変更できます。

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