Laziz Ergashev Laziz Ergashev - 3 months ago 13
Android Question

A Error With Going To Intent Android

package com.lazerg.apptask;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class tasks extends AppCompatActivity {
Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);

APlusBButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
APlubBGotoIntent();
}
});
}

void APlubBGotoIntent(){
Intent i = new Intent(getApplicationContext(),APlusBClass.class);
startActivity(i);
}
}


This is my code When I'm trying to move other intent Its saying

09-01 17:49:22.109 9259-9259/com.lazerg.apptask E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lazerg.apptask, PID: 9259
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lazerg.apptask/com.lazerg.apptask.tasks}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2989)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Answer

With out

setContentView(R.layout.activity_tasks);

How you can declare

 Button APlusBButton = (Button) findViewById(R.id.aplusbgoto);

You should declare button after ContentView because with view how you can use widget for that view.