My class does not find my layouts or activities android

1

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

    
asked by gery_08 05.12.2017 в 23:22
source

3 answers

1

You have an Activity but you have not specified the onCreate() method,  you must add it and later you can change the layout with

 setContentView(R.layout.<layout>)

The problem, you are calling the setContentView() of an Activity that is not created , in this case you call from the Activity: MainActivity you call the method update() of the Activity: check but this has not gone through onCreate() !

    
answered by 05.12.2017 / 23:26
source
1

As you mentioned, you do not respect the life cycle of an activity. Apparently you create the class first and then the layout and you have not created the link between these two components.

There is a way to generate the link between the class and the layout automatically, that is, you will generate the onCreate ... Example:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ejemplo);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
}

Simply follow these steps:

Paquete Java > Paquete de clases > New > Activity 

With another you create a clase and a XML with the same name and you can use it without problems, there you could do your validation for firebase .

    
answered by 06.12.2017 в 00:14
0

The problem is that you are not respecting the life cycles of an activity. The setContentView method should only be used when the onCreate and it is very likely that you are not doing it.

Also keep in mind that you should not update the layout of an activity dynamically, since you would have to add all the corresponding events to that view at the time of changing so the Fragment s.

    
answered by 05.12.2017 в 23:36