Reputation:
I have an android application which should opens an activity on button press, but when i click on itm the app crashes stating "Attempt to invoke virtual method". Here is my Code.
MainClass.java
public class MainActivity extends ActionBarActivity {
Button Notify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notify = (Button)findViewById(R.id.notify);
Notify.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
startActivity(intent);
}
});
}
My another activity class
DisplayPush.java
public class DisplayPush extends Activity {
@Override
public void onBackPressed(){
Intent myIntent = new Intent(DisplayPush.this,MainActivity.class);
startActivity(myIntent);
// overridePendingTransition(R.anim.from_middle, R.anim.to_middle);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
TextView notification_title = (TextView) findViewById(R.id.title);
TextView notification_message = (TextView) findViewById(R.id.message);
ParseAnalytics.trackAppOpened(getIntent());
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
try{
JSONObject notification = new JSONObject(jsonData);
// String title = notification.getString("push_hash");
String message = notification.getString("alert");
// notification_title.setText(title);
notification_message.setText(message);
}
catch(JSONException e){
Toast.makeText(getApplicationContext(), "Something went wrong with the notification", Toast.LENGTH_SHORT).show();
}
}
I have defined the acitivities in my manifest. Here is the logcat.
03-21 06:51:35.157: E/AndroidRuntime(3042): FATAL EXCEPTION: main
03-21 06:51:35.157: E/AndroidRuntime(3042): Process: hasan.parsereceiveandshow, PID: 3042
03-21 06:51:35.157: E/AndroidRuntime(3042): java.lang.RuntimeException: Unable to start activity ComponentInfo{hasan.parsereceiveandshow/hasan.parsereceiveandshow.DisplayPush}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
Upvotes: 0
Views: 941
Reputation: 132992
As in log:
Bundle.getString(java.lang.String)' on a null object reference
Because extras
is null
.
Not sending Bundle which contain com.parse.Data
key with Intent which is used for starting DisplayPush
Activity on Button Click.
What should i edit in code ?
Send data with com.parse.Data
key in Intent on Notify
Button click:
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);
And also add null check before getting values from Bundle:
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
String jsonData = extras.getString("com.parse.Data");
And define String jsonData;
globally as said in comments.
Upvotes: 2
Reputation: 3746
The exception says that you're trying to call getString
method on null reference. In DisplayPush
you are getting Bundle
object from intent which is null, because you didn't put any in your intent in onClick
method in MainActivity
class.
Upvotes: 1