Issue
Good day everyone, can somebody help me on how will I make this process? I'm just trying to test this on one button. Suppose that:
After click the btnEasy1, it will go to the ButtonFunctions class and set the necessary setting for the GameActivity class.
PS. I am try this for now on one button(btnEasy1) only for now
PPS. The error says that I am trying to invoke virtual method on a null object reference.
ImageButton btnEasy1, btnEasy2, btnEasy3, btnEasy4, btnEasy5, btnEasy6, btnEasy7, btnEasy8, btnEasy9, btnEasy10, btnBack;
int row = 0 , column = 0 , stage = 0;
ButtonFunctions btnFunc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level__easy);
this.btnEasy1 = findViewById(R.id.btnEasy1);
this.btnEasy2 = findViewById(R.id.btnEasy2);
this.btnEasy3 = findViewById(R.id.btnEasy3);
this.btnEasy4 = findViewById(R.id.btnEasy4);
this.btnEasy5 = findViewById(R.id.btnEasy5);
this.btnEasy6 = findViewById(R.id.btnEasy6);
this.btnEasy7 = findViewById(R.id.btnEasy7);
this.btnEasy8 = findViewById(R.id.btnEasy8);
this.btnEasy9 = findViewById(R.id.btnEasy9);
this.btnEasy10 = findViewById(R.id.btnEasy10);
this.btnBack = findViewById(R.id.btnBack);
//Easy Level intents
Intent easyLevelIntent = new Intent(getApplicationContext(), GameActivity.class);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Level_Selection.class);
startActivity(intent);
finish();
}
});
btnEasy1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnFunc =new ButtonFunctions();
btnFunc.functions("easy1");
finish();
}
});
public class ButtonFunctions extends AppCompatActivity {
int row, column, stage;
public void functions(String diffStage){
Intent easyLevelIntent = new Intent(getApplicationContext(), GameActivity.class);
if(diffStage.equals("easy1")){
stage = 1 ;row = 2; column = 2 ;
setRowCol(easyLevelIntent);
startActivity(easyLevelIntent);
}
}
private void setRowCol(Intent easyLevelIntent) {
easyLevelIntent.putExtra("easyStageCount", stage);
easyLevelIntent.putExtra("rowCount", row);
easyLevelIntent.putExtra("colCount", column);
easyLevelIntent.putExtra("difficulty", 1);
}
Process: com.flip, PID: 10543
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
at com.flip.Functions.ButtonFunctions.functions(ButtonFunctions.java:14)
at com.flip.StageSelection.Level_Easy$2.onClick(Level_Easy.java:54)
at android.view.View.performClick(View.java:5680)
at android.view.View$PerformClick.run(View.java:22650)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1076)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:937)
Solution
You have declared ButtonFunctions as extending AppCompatActivity. But this class is not an Activity, it is just a utility class. You are getting this NullPointerException because you have created an Activity using the new keyword. This is not allowed. Only the Android framework can correctly create new instances of Android components (Activity, Service, Provider).
You should remove the extends AppCompatActivity from the ButtonFunctions class declaration and add a Context parameter to any of the methods that need one. For example, this one:
public void functions(Context context, String diffStage){
Intent easyLevelIntent = new Intent(context, GameActivity.class);
if(diffStage.equals("easy1")){
stage = 1 ;row = 2; column = 2 ;
setRowCol(easyLevelIntent);
context.startActivity(easyLevelIntent);
}
}
Another approach would be to move all the methods from ButtonFunctions into your Activity, since they seem to be very dependent on it. But this is an architectural choice that you need to make.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.