Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

android - from one activity create multiple intents which point to different activities gives a nullpointer exception

Can I do sth like that?

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;

     }
     }

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.

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

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)

I forgot to out in manifest the " ". After that , when I start my application it crashes giving "nullpointer exception in IntentService".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot start your HttpClient in the main UI thread. You must create another Thread/Runnable in which to run your httpClient. In addition to your NullPointerException (once you solve that), you are going to receive a android.os.NetworkOnMainThreadException.

Move the httpClient into a Runnable to fix part of your problem.

In order to fix the NullPointerException, we need to see the LogCat showing the file and line number in which it occurs. You must post the line of code at that line number, because we cannot see line numbers in the question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...