无法进入下一个活动

时间:2015-03-29 16:34:15

标签: android

logcat的:

03-29 05:26:20.400: D/AndroidRuntime(1842): Shutting down VM
03-29 05:26:20.400: W/dalvikvm(1842): threadid=1: thread exiting with uncaught exception (group=0xb4a27b90)
03-29 05:26:20.440: E/AndroidRuntime(1842): FATAL EXCEPTION: main
03-29 05:26:20.440: E/AndroidRuntime(1842): Process: com.example.farmerssacco, PID: 1842
03-29 05:26:20.440: E/AndroidRuntime(1842): java.lang.NullPointerException
03-29 05:26:20.440: E/AndroidRuntime(1842):     at com.example.farmerssaccoproduce.AddProduce$AddProduct.onPostExecute(AddProduce.java:171)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at com.example.farmerssaccoproduce.AddProduce$AddProduct.onPostExecute(AddProduce.java:1)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.os.AsyncTask.finish(AsyncTask.java:632)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.os.Looper.loop(Looper.java:137)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-29 05:26:20.440: E/AndroidRuntime(1842):     at dalvik.system.NativeStart.main(Native Method)

代码:

公共类AddProduce扩展了Activity {

private ProgressDialog pDialog;
String productname,price,quantity,idnumber,returnedstring;

 private static String KEY_SUCCESS = "success";
 private static String KEY_ERROR = "error";

Spinner inputProductName;
EditText inputPrice;
EditText inputIdNumber;
EditText inputQuantity;

Button AddProduct;
TextView addErrorMsg;

Spinner Spnr ;
String[] products = {
    "Wheat",
    "Maize",
    "Millet",
    "Sorghum",
    "Potatoes",
    "Beans",
    "Peas"

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addproduce);

     /**
     * Defining all layout items
     **/


     Spnr = (Spinner)findViewById(R.id.spinner);
     inputProductName= (Spinner) findViewById(R.id.spinner);
    inputPrice = (EditText) findViewById(R.id.Prc);
    inputQuantity = (EditText)findViewById(R.id.qty);
    inputIdNumber = (EditText)findViewById(R.id.idnmber);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, products);
            Spnr.setAdapter(adapter);
            Spnr.setOnItemSelectedListener(
                      new AdapterView.OnItemSelectedListener() {
                          @Override
                          public void onItemSelected(AdapterView<?> arg0, View arg1,
                                  int arg2, long arg3) {
                            int position = Spnr.getSelectedItemPosition();
                            Toast.makeText(getApplicationContext(),"You have selected "+products[+position],Toast.LENGTH_LONG).show();
                              // TODO Auto-generated method stub
                          }
                          @Override
                          public void onNothingSelected(AdapterView<?> arg0) {
                              // TODO Auto-generated method stub
                          }
                      }
                  );


    Button AddProduct = (Button) findViewById(R.id.btnAddProduce);
     AddProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
             if (  ( !inputProductName.getSelectedItem().toString().equals("")) && ( !inputPrice.getText().toString().equals("")) && ( !inputIdNumber.getText().toString().equals("")) && ( !inputQuantity.getText().toString().equals("")))
                { 
                 NetAsync(view);

                }
             else
                {
                    Toast.makeText(getApplicationContext(),
                            "One or more fields are empty", Toast.LENGTH_SHORT).show();
                }

        }
    });
}

private class AddProduct extends AsyncTask <String,String,JSONObject> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();

         productname = inputProductName.getSelectedItem().toString();
         price = inputPrice.getText().toString();
         idnumber = inputIdNumber.getText().toString();
         quantity= inputQuantity.getText().toString();
            pDialog = new ProgressDialog(AddProduce.this);
            pDialog.setTitle("Contacting Servers");
            pDialog.setMessage("Adding ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

    }


    @Override
    protected JSONObject doInBackground(String... params) {

        // TODO Auto-generated method stub
        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.addProduct(productname,price,idnumber,quantity);
            return json;

    }

    @Override
    protected void onPostExecute(JSONObject json) {

        try{
            if (json.getString(KEY_SUCCESS) != null) {
            addErrorMsg.setText("");
            String res = json.getString(KEY_SUCCESS);
            String red = json.getString(KEY_ERROR);

            Intent upanel = new Intent(getApplicationContext(), HomePage.class);
            upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pDialog.dismiss();
            startActivity(upanel);
            /**
             * Close Login Screen
             **/
            finish();
        }else{
            pDialog.dismiss();
            addErrorMsg.setText("Error occured in adding");
        }

        }catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
 public void NetAsync(View view){
     new AddProduct().execute();
 }
}

1 个答案:

答案 0 :(得分:0)

我不知道171行是哪里。 但我认为问题应该在这里:

  Intent upanel = new Intent(getApplicationContext(), HomePage.class);

您应该提供当前的Activity引用而不是getApplicationContext()。因此,您可以从AddProduct构造中放置Activity引用