Java 应用程序在按下按钮时崩溃;尝试调用虚拟方法“;
我有一个android应用程序,它应该在按下按钮时打开一个活动,但当我点击itm时,该应用程序崩溃,声明“试图调用虚拟方法”。这是我的密码 MainClass.javaJava 应用程序在按下按钮时崩溃;尝试调用虚拟方法“;,java,android,android-intent,Java,Android,Android Intent,我有一个android应用程序,它应该在按下按钮时打开一个活动,但当我点击itm时,该应用程序崩溃,声明“试图调用虚拟方法”。这是我的密码 MainClass.java public class MainActivity extends ActionBarActivity { Button Notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa
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);
}
});
}
我的另一个活动课
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();
}
}
我已经在清单中定义了活动。
这是航海日志
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
如日志所示:
Bundle.getString(java.lang.String)' on a null object reference
因为extras
是null
不发送包含com.parse.Data
键的捆绑包,该键用于在单击按钮时启动displayphush
活动
我应该在代码中编辑什么
使用com.parse.data发送数据
在Notify
按钮上键入意图单击:
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);
并在从Bundle获取值之前添加空检查:
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
String jsonData = extras.getString("com.parse.Data");
定义字符串jsonData代码>如注释中所述的全局性。如日志中所述:
Bundle.getString(java.lang.String)' on a null object reference
因为extras
是null
不发送包含com.parse.Data
键的捆绑包,该键用于在单击按钮时启动displayphush
活动
我应该在代码中编辑什么
使用com.parse.data发送数据
在Notify
按钮上键入意图单击:
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);
并在从Bundle获取值之前添加空检查:
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
String jsonData = extras.getString("com.parse.Data");
定义字符串jsonData代码>如注释中所述的全局性。如日志中所述:
Bundle.getString(java.lang.String)' on a null object reference
因为extras
是null
不发送包含com.parse.Data
键的捆绑包,该键用于在单击按钮时启动displayphush
活动
我应该在代码中编辑什么
使用com.parse.data发送数据
在Notify
按钮上键入意图单击:
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);
并在从Bundle获取值之前添加空检查:
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
String jsonData = extras.getString("com.parse.Data");
定义字符串jsonData代码>如注释中所述的全局性。如日志中所述:
Bundle.getString(java.lang.String)' on a null object reference
因为extras
是null
不发送包含com.parse.Data
键的捆绑包,该键用于在单击按钮时启动displayphush
活动
我应该在代码中编辑什么
使用com.parse.data发送数据
在Notify
按钮上键入意图单击:
Intent intent = new Intent(MainActivity.this, DisplayPush.class);
intent.putExtra("com.parse.Data","String text");
startActivity(intent);
并在从Bundle获取值之前添加空检查:
if(extras !=null)
if(extras.containsKey("com.parse.Data"))
String jsonData = extras.getString("com.parse.Data");
定义字符串jsonData
全局,如注释中所述。异常表示您正试图调用null引用上的getString
方法。在DisplayPush
中,您正从intent获取Bundle
对象,该对象为null,因为您没有在onClick
方法的MainActivity
类中的intent中添加任何内容。异常表示您试图调用null引用上的getString
方法。在DisplayPush
中,您正从intent获取Bundle
对象,该对象为null,因为您没有在onClick
方法的MainActivity
类中的intent中添加任何内容。异常表示您试图调用null引用上的getString
方法。在DisplayPush
中,您正从intent获取Bundle
对象,该对象为null,因为您没有在onClick
方法的MainActivity
类中的intent中添加任何内容。异常表示您试图调用null引用上的getString
方法。在DisplayPush
中,您从intent获得的Bundle
对象为空,因为您没有在MainActivity
类中的onClick
方法中添加任何内容。@penta:addnull
检查并使用extras.containsKey
检查密钥是否存在于Bundle中,然后再访问值我应该在哪里键入Bundle extras=intent.getExtras()…在我的code@penta:紧跟在Intent-Intent=getIntent()之后代码>line@penta:哪个字符串未解析?全局声明所有字符串并在onCreate中使用method@penta:在访问值之前,添加null
检查并使用extras.containsKey
检查密钥是否存在于捆绑包中,我应该在我的code@penta:紧跟在Intent-Intent=getIntent()之后代码>line@penta:哪个字符串未解析?全局声明所有字符串并在onCreate中使用method@penta:在访问值之前,添加null
检查并使用extras.containsKey
检查密钥是否存在于捆绑包中,我应该在我的code@penta:紧跟在Intent-Intent=getIntent()之后代码>line@penta:哪个字符串未解析?全局声明所有字符串并在onCreate中使用method@penta:在访问值之前,添加null
检查并使用extras.containsKey
检查密钥是否存在于捆绑包中,我应该在我的code@penta:紧跟在Intent-Intent=getIntent()之后代码>line@penta:哪个字符串未解析?全局声明所有字符串并在onCreate方法中使用