dimanche 17 mai 2015

My object work only in onCreate()

I have a object Model and it is creating in first activity and transferred by Intent to second activity. When object is initialized in second activity I can use it only in onCreate(), but when I use in other method app crashing.

1-st Activity creating object and add to intent

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

2-nd activity

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

Thanks for help :)

Aucun commentaire:

Enregistrer un commentaire