-1

I've MainActivity as following:

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());
}
}

I've another activity named main2activity. But when I press the button in 1st activity, The app crashes and log gives NullPointerException. But this is not duplicate of any other such NullPointerException questions.

Because when I run only my second activity Main2Activity, separately, it does not give to this error. But when It is called from mainActivity, It is giving error. I can't understand why setting listener on button giving NullPointerException.

My Main2Activity is :

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();
}  

Here I've mentioned where the error is occurring in comment. And I've other methods with this but only included necessary part.

Thanks in advance!

EDIT: Log:

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 Answers1

0

You are setting the same layout in both activities. Make sure and add correct layout.

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 
   }
 }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • Ohh! Thanks That was a stupid mistake. But Now it is giving same error in `MainActivity` where I'm setting the text to button i.e at this line: ` textView.setText("No Reminders");` –  Aug 18 '16 at 16:24
  • 1
    Okay It is solved. I had same ids of text views. Thank You very much for the help. Can You answer this question? Thanks in advance. http://stackoverflow.com/questions/39016285/how-to-retrieve-snapshot-of-map-from-place-picker-activity –  Aug 18 '16 at 16:30