0

I am passing my data from one activity to another activity via intent using a bundle.now this bundle is received by another activity here i am gonna display the values i passed.now when the a button is clicked it is suppose to fire a interface which has a function .There it is showing a null point error.

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

Error

FATAL EXCEPTION: main Process: com.matrix.storm.question, PID: 27805 java.lang.NullPointerException at com.matrix.storm.question.WILO$1.onClick(WILO.java:34) at android.view.View.performClick(View.java:4452) at android.view.View$PerformClick.run(View.java:18451) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5421) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) at dalvik.system.NativeStart.main(Native Method)

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
  • You don't need two activities for this because there is no UI in the 1st activity. So you should create and display the results of the timer in the same activity only. In onFinish() you should update the UI rather that calling 2nd activity. – anubh Mar 16 '17 at 18:57

1 Answers1

-1

How to use interface between two activities

You can not connect two activities using interfaces. If you want to send receive data between two activities, you can use the help of intent arguments and ActivityResult method.

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

       }
Darish
  • 11,032
  • 5
  • 50
  • 70