如何发送数据

时间:2019-06-27 12:55:40

标签: android android-intent

我正在尝试使用Extras将数据从ChronometerActivity发送到PlayerHomeActivity,但这是行不通的。

计时器活动:

public class ChronometerActivity extends AppCompatActivity {

@BindView(R.id.chronometer)
Chronometer chronometer;
@BindView(R.id.fab_play) FloatingActionButton fab_play;
@BindView(R.id.fab_pause) FloatingActionButton fab_pause;
@BindView(R.id.fab_stop) FloatingActionButton fab_stop;
@BindView(R.id.btn_player) Button btn_player;
@BindView(R.id.btn_golkeeper) Button btn_goolkeeper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chronometr);
    ButterKnife.bind(this);

    timeWhenStopped=0;
    ChronometerListener listener = new ChronometerListener(this);
    fab_play.setOnClickListener(listener);
    fab_pause.setOnClickListener(listener);
    fab_stop.setOnClickListener(listener);
    btn_player.setOnClickListener(listener);
    btn_goolkeeper.setOnClickListener(listener);

}

PlayerHomeActivity:

public class PlayerHomeActivity extends AppCompatActivity {

private int typePlayer = getIntent().getExtras().getInt("typePlayer");


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_home);

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    PlayerHomeActivitylistener listener = new PlayerHomeActivitylistener(this);
    navigation.setOnNavigationItemSelectedListener(listener);

    .....
  }
}

ChronometerListener:

public class ChronometerListener implements View.OnClickListener {
ChronometerActivity activity;
public ChronometerListener(ChronometerActivity activity) {
    this.activity=activity;
}

@Override
public void onClick(View v) {
    String typePlayer;
    switch (v.getId()){

        case R.id.fab_play:
            activity.start(activity.getChronometer());
            break;
        case R.id.fab_stop:
            activity.stop(activity.getChronometer());
            break;
        case R.id.fab_pause:
            activity.pause(activity.getChronometer());
            break;
        case R.id.btn_player:
            typePlayer="0";
            Intent intent =new Intent(activity.getApplicationContext(), PlayerHomeActivity.class);

            intent.putExtra("typePlayer", typePlayer);
            activity.startActivity(intent);
            break;
        case R.id.btn_golkeeper:
            typePlayer="1";
            Intent intent2 = new Intent(activity.getApplicationContext(), PlayerHomeActivity.class);
            intent2.putExtra("typePlayer", typePlayer);
            activity.startActivity(intent2);
            break;

    }

}

我收到此错误:

  

E / Android运行时:致命异常:主要       进程:simonerusso.it.beneventoscouting,PID:5963       java.lang.RuntimeException:无法实例化活动ComponentInfo {simonerusso.it.beneventoscouting / simonerusso.it.beneventoscouting.view.PlayerHomeActivity}:   java.lang.NullPointerException:尝试调用虚拟方法   'android.os.Bundle android.content.Intent.getExtras()'为null   对象参考           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)           在android.app.ActivityThread.access $ 800(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)        引起原因:java.lang.NullPointerException:尝试调用虚拟方法'android.os.Bundle android.content.Intent.getExtras()'   在空对象引用上           在simonerusso.it.beneventoscouting.view.PlayerHomeActivity。(PlayerHomeActivity.java:116)           在java.lang.reflect.Constructor.newInstance(本地方法)           在java.lang.Class.newInstance(Class.java:1606)           在android.app.Instrumentation.newActivity(Instrumentation.java:1066)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)           在android.app.ActivityThread.access $ 800(ActivityThread.java:151)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

2 个答案:

答案 0 :(得分:1)

public class PlayerHomeActivity extends AppCompatActivity {


 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_home);


 int typePlayer = getIntent().getExtras().getInt("typePlayer");

BottomNavigationView navigation = (BottomNavigationView) 
findViewById(R.id.navigation);
PlayerHomeActivitylistener listener = new PlayerHomeActivitylistener(this);
navigation.setOnNavigationItemSelectedListener(listener);

.....
   }
}

移动到oncreate()内部的行下方

 int typePlayer = getIntent().getExtras().getInt("typePlayer");

答案 1 :(得分:0)

您需要在Activity的getIntent().getExtras().getInt("typePlayer");内致电onCreate()