My application is connected to a firebase server, and I have created a class to check if the data has been changed and, if so, go to a specific layout or activity:
package com.example.gerard.presentacio;
import com.example.gerard.presentacio.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
/**
* Created by Gerard on 2/12/2017.
*/
public class check extends Activity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("num");
public String id;
private int diapo;
private static Context context;
public void checkeja(){
if(diapo==1){
setContentView(R.layout.diapo1);
}
if(diapo==2){
setContentView(R.layout.diapo2);
}
if(diapo==3){
Intent i = new Intent(getBaseContext(), Nom.class);
startActivity(i);
}
if(diapo==4){
Intent i = new Intent(getBaseContext(), edat.class);
startActivity(i);
}
}
public void update() {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
//TextView mostra = (TextView)findViewById(R.id.actual);
int value = dataSnapshot.getValue(int.class);
diapo=value;
checkeja();
}
@Override
public void onCancelled(DatabaseError error) {
// Toast.makeText(MainActivity.class, "Error de connecció", Toast.LENGTH_SHORT).show();
}
});
}
}
But when running the application it stops and I miss the following errors saying that the layouts and activities are not found:
E / AndroidRuntime: FATAL EXCEPTION: main Process: com.example.gerard.presentacio, PID: 25522 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Window.setContentView (int)' on a null object reference at android.app.Activity.setContentView (Activity.java:2548) at com.example.gerard.presentacio.check.checkeja (check.java:27) at com.example.gerard.presentacio.check $ 1.onDataChange (check.java:51) at com.google.android.gms.internal.to.zza (Unknown Source) at com.google.android.gms.internal.vj.zzHX (Unknown Source) at com.google.android.gms.internal.vp.run (Unknown Source) at android.os.Handler.handleCallback (Handler.java:751) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6776) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1496) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386) Application terminated.
I tried using the same code in the MainActivity and it works well, I've been looking around here to see if someone else had happened but I've seen that I'm the only one, so I guess it will be that I have not extended the class well or some other basic error.
Many thanks in advance