从一个活动创建一个指向不同的活动多的意图给出了一个空指针异常 [英] from one activity create multiple intents which point to different activities gives a nullpointer exception

查看:95
本文介绍了从一个活动创建一个指向不同的活动多的意图给出了一个空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能折腾成这样?

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编辑文本字段上述活动和一个按钮,我希望在按钮pssed到code_value到评选活动中,TIME_VALUE通为了MyService活动,并开始主要活动$ P $。

From the above activity which has 2 edit text fields and a button I want when the button is pressed to pass the "code_value" to selection activity , time_value to myservice activity and start main activity.

这样做,让我评选活动中一个空指针异常OnCreate中。
不得不提的是,如果我只使用意向我和它传递了两个值,然后startactivity(I),它工作正常。

Doing that gives me a nullpointer exception in selection activity oncreate. I must mention that if I use only Intent i and pass the 2 values in it and then startactivity(i),it works fine.

---------------------更新------------------------- -----------------

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

Selection类:

Selection class:

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的:

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。

推荐答案

好吧,问题是,我收到了code_value字符串oncreate.Moved它,现在它OJK。

Ok, the problem was that I had the code_value string before the oncreate.Moved it and its ojk now.

这篇关于从一个活动创建一个指向不同的活动多的意图给出了一个空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆