当Intent通过时,应用程序崩溃

时间:2016-08-18 16:10:15

标签: java android android-intent nullpointerexception

我' MainActivity如下:

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    myDb =new DatabaseHelper(this);
    Cursor res= myDb.getAllData();
    if(res.getCount()==0)
    {
        textView = (TextView)findViewById(R.id.textView);
        textView.setText("No Reminders");
        button = (Button)findViewById(R.id.button);
        button.setText("Create a Reminder");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {   //on click 2nd activity is called.
                Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
                //myIntent.putExtra("key", value); //Optional parameters
                startActivity(myIntent);
            }
        });

        return;
    }
    StringBuffer buffer=new StringBuffer();
    while(res.moveToNext())
    {
        buffer.append("Id : " +res.getString(0)+"\n");
        buffer.append("Event : " +res.getString(1)+"\n");
        buffer.append("Location : " +res.getString(2)+"\n");
    }
    textView = (TextView)findViewById(R.id.textView);
    textView.setText(buffer.toString());
}
}

我还有另一个名为main2activity的活动。但是当我在第一个活动中按下按钮时,应用程序崩溃并记录日志NullPointerException但这不是任何其他此类NullPointerException问题的重复

因为当我仅运行第二个活动Main2Activity时,它不会给出此错误。但是当从mainActivity调用它时,它会给出错误。我无法理解为什么在按钮上设置监听器NullPointerException

我的Main2Activity是:

public class Main2Activity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private GoogleApiClient mGoogleApiClient;
public TextView tv4;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
        new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));



private boolean flag = false;
DatabaseHelper myDb;
EditText newevent;
Button submit;
Button viewremainders;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Intent intent = getIntent();


    setContentView(R.layout.activity_main);
    myDb =new DatabaseHelper(this);
    newevent=(EditText)findViewById(R.id.newEvent);
    submit=(Button)findViewById(R.id.submit);
    viewremainders=(Button)findViewById(R.id.view);

    Button pickerButton = (Button) findViewById(R.id.pickerButton);
    tv4 = (TextView)findViewById(R.id.textView4);
    pickerButton.setOnClickListener(new View.OnClickListener() {  // <-- Error is here. (NullPointerException.)
        @Override
        public void onClick(View v) {
            try {
                PlacePicker.IntentBuilder intentBuilder =
                        new PlacePicker.IntentBuilder();
                intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
                Intent intent = intentBuilder.build(Main2Activity.this);
                startActivityForResult(intent, PLACE_PICKER_REQUEST);

            } catch (GooglePlayServicesRepairableException
                    | GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });
    AddData();
    viewremainders();
}  

我在这里提到了评论中发生错误的位置。我还有其他方法,但只包括必要的部分。

提前致谢!

修改: 经度:

08-18 21:48:05.421 29655-29655/com.example.kaushal28.locationbasedreminder E/test: Exception
08-18 21:48:05.424 29655-29655/com.example.kaushal28.locationbasedreminder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kaushal28.locationbasedreminder/com.example.kaushal28.locationbasedreminder.Main2Activity}: java.lang.NullPointerException
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
                                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:156)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                             at android.os.Looper.loop(Looper.java:153)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                                          Caused by: java.lang.NullPointerException
                                                                                             at com.example.kaushal28.locationbasedreminder.Main2Activity.onCreate(Main2Activity.java:85)
                                                                                             at android.app.Activity.performCreate(Activity.java:5182)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358) 
                                                                                             at android.app.ActivityThread.access$600(ActivityThread.java:156) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                             at android.os.Looper.loop(Looper.java:153) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5299) 
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
                                                                                             at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您在两个活动中设置相同的布局。确保并添加正确的布局。

setContentView(R.layout.activity_main);

public class Main2Activity extends AppCompatActivity {

.
.
.

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    setContentView(R.layout.activity_main); // here is issue same layout is used for both activity 
   }
 }