启动活动时出现nullpointer异常

时间:2012-10-20 13:28:40

标签: android exception android-intent android-emulator nullpointerexception

当我开始以下活动时,我正在使用android我在我的android模拟器上有一个强制关闭。我认为这是由于getstringExtra()中的null值,但我无法正确找到它。任何人都可以说这个活动中的错误是什么

public class Cancel_parkActivity extends Activity implements LocationListener{


        String p_u_name;
        HttpPost httppost;
        StringBuffer buffer;
        HttpResponse response;
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs;
        ProgressDialog dialog = null;
        LocationManager locManager;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.cancel_park);

              Intent in = getIntent();
              p_u_name = in.getExtras().getString("KEY_USERNAME");

                  System.out.println(" Cancel_park : your name is"+p_u_name);

                    dialog = ProgressDialog.show(Cancel_parkActivity.this, "", 
                            "Cancel parking...", true);
                     new Thread(new Runnable() {
                            public void run() {
                                cancelpark();                          
                            }
                          }).start(); 

                    }

        public void cancelpark(){
             try{  
                 httpclient=new DefaultHttpClient();
                 httppost= new HttpPost("http://10.0.2.2/test_login/deletepark.php");

                 nameValuePairs = new ArrayList<NameValuePair>(2);

                String KEY_DELETE = "delete";
                 nameValuePairs.add(new BasicNameValuePair(KEY_DELETE,""));
                 nameValuePairs.add(new BasicNameValuePair("username",p_u_name.trim()));
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                 final String response = httpclient.execute(httppost, responseHandler);
                 System.out.println("Response : " + response); 
                 runOnUiThread(new Runnable() {
                     public void run() {
                        System.out.println("Response from PHP : " + response);
                         dialog.dismiss();
                     }
                 });

                 if(response.equalsIgnoreCase("Success")){
                     runOnUiThread(new Runnable() {
                         public void run() {
                             Toast.makeText(Cancel_parkActivity.this,"Last parking canceled", Toast.LENGTH_SHORT).show();

                         }
                     });

//                   startActivity(new Intent(LoginActivity.this, MainActivity.class));
                 }else{
                     Toast.makeText(Cancel_parkActivity.this,"fucking", Toast.LENGTH_SHORT).show();

                 }

             }catch(Exception e){
                 dialog.dismiss();
                 System.out.println("Exception : " + e.getMessage());
             }
         }
         public void showAlert(){
             Cancel_parkActivity.this.runOnUiThread(new Runnable() {
                 public void run() {
                     AlertDialog.Builder builder = new AlertDialog.Builder(Cancel_parkActivity.this);
                     builder.setTitle("Parking Error.");
                     builder.setMessage("Try again?.")  
                            .setCancelable(false)
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    Intent r = new Intent(getApplicationContext(),Park_HereActivity.class);
                                    startActivity(r);
                                }
                            });                     
                     AlertDialog alert = builder.create();
                     alert.show();               
                 }
             });

         }
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub

        }
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
    }

我得到了像这样的nullpointer异常

10-20 17:42:50.044: E/AndroidRuntime(1983): FATAL EXCEPTION: main
10-20 17:42:50.044: E/AndroidRuntime(1983): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oranz.epark/com.oranz.epark.Cancel_parkActivity}: java.lang.NullPointerException
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.os.Looper.loop(Looper.java:137)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread.main(ActivityThread.java:4340)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at java.lang.reflect.Method.invoke(Method.java:511)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at dalvik.system.NativeStart.main(Native Method)
10-20 17:42:50.044: E/AndroidRuntime(1983): Caused by: java.lang.NullPointerException
10-20 17:42:50.044: E/AndroidRuntime(1983):     at com.oranz.epark.Cancel_parkActivity.onCreate(Cancel_parkActivity.java:43)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.Activity.performCreate(Activity.java:4465)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-20 17:42:50.044: E/AndroidRuntime(1983):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-20 17:42:50.044: E/AndroidRuntime(1983):     ... 11 more

3 个答案:

答案 0 :(得分:1)

在代码中检查此行:

Cancel_parkActivity.java:43

也许getIntent()。getExtras()为空?

答案 1 :(得分:1)

Intent myIntent= getIntent(); // gets the previously created intent
    String firstKeyName = intent.getStringExtra("KEY_USERNAME");

试试这种方式。

由于

答案 2 :(得分:0)

p_u_name = getIntent().getStringExtra("KEY_USERNAME");

p_u_name throw NullPointer so please check the before Activity class
Example :

Intent intent = new Intent(this, Cancel_parkActivity.class);
intent.putExtra("KEY_USERNAME", your_Value);
startActivity(intent);

请检查以上编码,然后您可以轻松找到解决方案