Android 我的对象只有在onCreate()中实例化时才能工作
我有一个对象模型,它在第一个活动中创建,并通过意图转移到第二个活动。当对象在第二个活动中初始化时,我只能在onCreate中使用它,但当我在另一个方法中使用它时,应用程序崩溃 第一个创建对象并添加到意图的活动:Android 我的对象只有在onCreate()中实例化时才能工作,android,android-intent,Android,Android Intent,我有一个对象模型,它在第一个活动中创建,并通过意图转移到第二个活动。当对象在第二个活动中初始化时,我只能在onCreate中使用它,但当我在另一个方法中使用它时,应用程序崩溃 第一个创建对象并添加到意图的活动: Intent intent = new Intent(ModelCreatorActivity.this, ModelCreatorBluetooth.class); Model m = new Model(name); m.setStreamType(Model.StreamType.
Intent intent = new Intent(ModelCreatorActivity.this, ModelCreatorBluetooth.class);
Model m = new Model(name);
m.setStreamType(Model.StreamType.valueOf(type));
intent.putExtra("Model_new", new Gson().toJson(m));
ModelCreatorActivity.this.startActivity(intent);
第二活性
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_model_creator_bluetooth);
String jsonMyObject = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
jsonMyObject = extras.getString("Model_new");
}
m = new Gson().fromJson(jsonMyObject, Model.class);
//here normal work
Log.d("Models", m.getName() );
}
public void connectToDevice(String address) {
if (btt != null) {btt = null;
}
btt = new BluetoothThread(address, new Handler() {
@Override
public void handleMessage(Message message) {
Context c = getApplicationContext();
String s = (String) message.obj;
s = s.replace("\n", "").replace("\r", "");
if (s.equalsIgnoreCase("CONNECTED"))
{
Toast t = Toast.makeText(c, "CON", Toast.LENGTH_LONG);
t.show();
//here are crashing
Log.d("Models", m.getName() );
}
else if (s.equals("DISCONNECTED"))
{
Toast t = Toast.makeText(c, "DSC", Toast.LENGTH_LONG);
t.show();
}
else if (s.equals("CONNECTION FAILED"))
{
t.show();
btt = null;
}
}
});
}
谢谢您的帮助:也粘贴异常日志。告诉我们这是什么类型的异常?类名调用connectToDeviceString地址的位置?问题已解决。这是我在反对中犯错误的结果。我错误地创建了一个set方法。