0

次のアクティビティを開始すると、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
4

3 に答える 3

1

コードで次の行を確認してください。

Cancel_parkActivity.java:43

多分 getIntent().getExtras() は null ですか?

于 2012-10-20T13:29:41.563 に答える
1
Intent myIntent= getIntent(); // gets the previously created intent
    String firstKeyName = intent.getStringExtra("KEY_USERNAME");

この方法を試してください。

ありがとう

于 2012-10-20T13:35:32.753 に答える
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);

上記のコーディングを確認してください。そうすれば、簡単に解決策を見つけることができます。

于 2012-10-20T14:41:44.707 に答える