NullPionter通过Bundle传递字符串时发生异常

时间:2014-12-29 02:49:38

标签: java android nullpointerexception

我试图在我的应用程序中将一个String表单传递给另一个,但是我一直得到一个空指针错误。这是我的LogCat中显示的内容:

12-29 02:49:03.256: D/(663): HostConnection::get() New Host Connection established 0x96cb850, tid 663
12-29 02:49:06.288: W/KeyCharacterMap(663): No keyboard for id 0
12-29 02:49:06.288: W/KeyCharacterMap(663): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-29 02:49:09.600: D/AndroidRuntime(663): Shutting down VM
12-29 02:49:09.600: W/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0xb5f444f0)
12-29 02:49:09.604: E/AndroidRuntime(663): FATAL EXCEPTION: main
12-29 02:49:09.604: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld3/com.example.helloworld3.FloorPlan}: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.os.Looper.loop(Looper.java:130)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 02:49:09.604: E/AndroidRuntime(663):  at java.lang.reflect.Method.invokeNative(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663):  at java.lang.reflect.Method.invoke(Method.java:507)
12-29 02:49:09.604: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 02:49:09.604: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 02:49:09.604: E/AndroidRuntime(663):  at dalvik.system.NativeStart.main(Native Method)
12-29 02:49:09.604: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException
12-29 02:49:09.604: E/AndroidRuntime(663):  at com.example.helloworld3.FloorPlan.onCreate(FloorPlan.java:24)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 02:49:09.604: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 02:49:09.604: E/AndroidRuntime(663):  ... 11 more
12-29 02:49:12.316: D/(677): HostConnection::get() New Host Connection established 0x96c5790, tid 677

这是我的ManActivity.java:

package com.example.helloworld3;



import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts.Data;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText destination = (EditText)findViewById(R.id.roomdinput);

        final Button floorPlan = (Button)findViewById(R.id.floorPlanButton);        




floorPlan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){


     String roomName = destination.getText().toString();
    Bundle myb = new Bundle();
    myb.putString("key1", roomName);
    Intent a = new Intent(MainActivity.this, FloorPlan.class);
    a.putExtras(myb);
    startActivity(a);

    startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));



}   

});

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的FloorPan.java:

package com.example.helloworld3;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FloorPlan extends Activity{

    DrawView2 drawView;
    String roomName2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.floorplan);
        Bundle myb2= getIntent().getExtras();
        roomName2 = myb2.getString("key1");
        drawView = new DrawView2(this);

            drawView.setBackgroundResource(R.drawable.tait1st);
           setContentView(drawView);

    }
    public class DrawView2 extends View {
        Paint paint = new Paint();
        float ux, dx, rx,lx;




    public String getRoomName(){
        return roomName2;
    }
    public void setCoordinates(){


    if(roomName2 == "C154"){

        ux =90;
        dx = 250;
        rx = 90; 
        lx = 400;
    }else {
        ux =76;
        dx = 98;
        rx = 140; 
        lx = 300;

    }

    };
        public DrawView2(Context context) {
            super(context);
            paint.setColor(Color.RED);
            //roomName2 = drawView.getTag();

        }

        @Override
        public void onDraw(Canvas canvas) {
                canvas.drawLine(ux, dx , rx, lx, paint);
                canvas.drawLine(20, 0, 0, 20, paint);
                canvas.drawCircle(150, 400, 30, paint);
        }

    }
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

putString方法是正确的。它继承自android.os.BaseBundle。 logcat显示你的bundle来自intent.getExtras()是null:

Bundle myb2= getIntent().getExtras();
roomName2 = myb2.getString("key1");//here myb2 is null

也许你应该将onNewIntent重写为setIntent:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

答案 1 :(得分:0)

我认为,你的MainActivity中的floorPlan的OnClickListener中的这一行会导致问题:

startActivity(new Intent("com.example.helloworld3.FLOORPLAN"));

答案 2 :(得分:0)

为什么你在以下代码中开始了2次活动?

     Intent a = new Intent(MainActivity.this, FloorPlan.class); 
      a.putExtras(myb); startActivity(a);  
      startActivity(new Intent("com.example.helloworld3.FLOORPLAN")); // this intent doesnt`t have bundle

这可能是导致Bundle对象在intent中为null并导致nullpointerexception的原因。