从一个活动创建多个意图,指向不同的活动给出一个nullpointer异常

时间:2013-03-27 23:36:59

标签: android

我能这样做吗?

public void onClick(View v) {
        switch (v.getId()){
        case R.id.btn:
             edit11=edit1.getText().toString();
             edit22=edit2.getText().toString();
             Intent i=new Intent(this,selection.class);
             i.putExtra("code_value",edit11);
             Intent k=new Intent(this,myservice.class);
             k.putExtra("time_value",edit22);
                     this.startService(k);
             Intent l=new Intent(this,MainActivity.class);

             startActivity(l);  
             break;

     }
     }

从上面的活动中有2个编辑文本字段和按下按钮时我想要的按钮将“code_value”传递给选择活动,time_value传递给myservice活动并启动主要活动。

这样做会在选择活动oncreate中给出一个nullpointer异常。 我必须提一下,如果我只使用Intent i并在其中传递2个值然后启动活动(i),它就可以正常工作。

--------------------- UPDATE ------------------------- -----------------

选择课程:

public class selection extends FragmentActivity implements OnMyLocationChangeListener{

    GoogleMap googleMap;
    double latitude=0.0;  
    double longitude=0.0;
    String code_value="";


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


        code_value=getIntent().getExtras().getString("code_value");
         ...//code for google map
      ...
      public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_normal){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
         ....
          else if(checkedId == R.id.save_location){

                 new Thread(new Runnable() {
                     public void run() {

                           postData(Double.toString(latitude),Double.toString(longitude),code_value);
                     }

                 }).start();
        .....
         public void postData(String lat, String lo ,String code) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
                HttpGet  htget = new HttpGet("www.test.com"+lat+"/"+lo+"/"+code);

    try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(htget);
        String resp = response.getStatusLine().toString();
        Toast.makeText(this, resp, 5000).show();


    } catch (ClientProtocolException e) {
        Toast.makeText(this, "Error", 5000).show();
    } catch (IOException e) {
        Toast.makeText(this, "Error", 5000).show();
    }

logcat的:

W/dalvikvm(2459): threadid=1: thread exiting with uncaught exception (group=0x2aacc560)
03-28 10:34:35.478: E/AndroidRuntime(2459): Uncaught handler: thread main exiting due to uncaught exception
03-28 10:34:35.498: W/System.err(2459): java.io.IOException: Permission denied
03-28 10:34:35.508: E/AndroidRuntime(2459): FATAL EXCEPTION: main
03-28 10:34:35.508: E/AndroidRuntime(2459): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.datagps/com.example.datagps.selection}: java.lang.NullPointerException
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.os.Looper.loop(Looper.java:130)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at dalvik.system.NativeStart.main(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459): Caused by: java.lang.NullPointerException
03-28 10:34:35.508: E/AndroidRuntime(2459):     at com.example.datagps.selection.<init>(selection.java:87)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at java.lang.Class.newInstanceImpl(Native Method)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at java.lang.Class.newInstance(Class.java:1409)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-28 10:34:35.508: E/AndroidRuntime(2459):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)

我忘记了“”。 之后,当我启动我的应用程序时,它崩溃,在IntentService中给出“nullpointer异常”。

2 个答案:

答案 0 :(得分:1)

您无法在主UI线程中启动HttpClient。您必须创建另一个Thread / Runnable来运行httpClient。除了NullPointerException(一旦解决了),您将收到android.os.NetworkOnMainThreadException

httpClient移至Runnable以解决部分问题。

为了修复NullPointerException,我们需要看到LogCat显示它出现的文件和行号。您必须在该行号上发布代码行,因为我们无法在问题中看到行号。

答案 1 :(得分:0)

好的,问题是我在oncreate之前有了code_value字符串。现在移动它和它的ojk。