如何使用两个活动之间的接口

时间:2017-03-16 18:39:25

标签: java android

我通过意图使用bundle将我的数据从一个活动传递到另一个活动。现在这个包被另一个活动接收我将显示我通过的值。现在当点击一个按钮时,它被认为是开火一个具有函数的接口。它显示一个零点错误。

MainActivity.class

public class MainActivity extends AppCompatActivity implements WILO.Communicator {

int Tap=0,loss=9;

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

public void CountDowntimer()
{
    new CountDownTimer(3000, 1000) {
        @Override
        public void onTick(long millisUntilFinished)
        {
          Tap+=1;
            loss-=2;
        }

        @Override
        public void onFinish()
        {
            Bundle arg=new Bundle();
            arg.putInt("Tap",Tap);
            arg.putInt("Loss",loss);
            Intent i=new Intent(getBaseContext(),WILO.class);
            i.putExtras(arg);
            startActivity(i);
        }
    }.start();

}

@Override
public void Restart()
{
 CountDowntimer();
}
}

WILO.class

public class WILO extends Activity {

Communicator communicator;

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

    Bundle arg=getIntent().getExtras();

    Button Restart;
    TextView Tap,Loss;
    Restart= (Button) findViewById(R.id.Restart);
    Tap= (TextView) findViewById(R.id.Tap);
    Loss= (TextView) findViewById(R.id.Loss);

    Loss.setText(String.valueOf(arg.getInt("Loss")));
    Tap.setText(String.valueOf(arg.getInt("Tap")));

    Restart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            communicator.Restart();
            finish();
        }
    });
}
interface Communicator
{
    public void Restart();
}
}

错误

  

致命的例外:主要                                                                                处理:com.matrix.storm.question,PID:27805                                                                              显示java.lang.NullPointerException                                                                                  在com.matrix.storm.question.WILO $ 1.onClick(WILO.java:34)                                                                                  在android.view.View.performClick(View.java:4452)                                                                                  在android.view.View $ PerformClick.run(View.java:18451)                                                                                  在android.os.Handler.handleCallback(Handler.java:733)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:136)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5421)                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)                                                                                  在java.lang.reflect.Method.invoke(Method.java:515)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:979)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)                                                                                  在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:-1)

  

如何使用两个活动之间的接口

您无法使用接口连接两个活动。如果要在两个活动之间发送接收数据,可以使用intent参数和ActivityResult方法的帮助。

    Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
                    startActivityForResult(intent, 2);

      @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  

       }