I want all items from firebase database, so I used recyclerview to populate this items. When the activity with recylerview is called, app crashes with fatal error.
This is my code
My java file
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.Firebase;
import com.firebase.geofire.GeoFire;
import com.firebase.ui.FirebaseListAdapter;
import com.firebase.ui.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RequestPendingActivity extends AppCompatActivity {
RecyclerView RequestListView;
Firebase ref = new Firebase("https://simplyparc.firebaseio.com/Valet Requests Location");
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request_pending);
RequestListView = (RecyclerView) findViewById(R.id.RequestListView);
RequestListView.setHasFixedSize(true);
RequestListView.setLayoutManager(new LinearLayoutManager(this));
GeoFire geoFire = new GeoFire(mDatabase);
}
@Override
protected void onStart(){
super.onStart();
FirebaseRecyclerAdapter<String, MessageViewHolder> adapter = new FirebaseRecyclerAdapter<String, MessageViewHolder>(String.class, android.R.layout.two_line_list_item, MessageViewHolder.class, ref) {
@Override
protected void populateViewHolder(MessageViewHolder messageViewHolder, String s, int i) {
messageViewHolder.mText.setText(s);
}
};
RequestListView.setAdapter(adapter);
}
public static class MessageViewHolder extends RecyclerView.ViewHolder{
TextView mText;
public MessageViewHolder(View v){
super(v);
mText = (TextView) v.findViewById(android.R.id.text1);
}
}
}
My android Manifest file
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".FirstActivity"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PendingSelectorActivity"
android:label="Select Pending task"
android:screenOrientation="portrait"/>
<activity
android:name=".RequestPendingActivity"
android:label="Pending Request"
android:screenOrientation="portrait"/>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
<activity
android:name=".RequestAcceptMapActivity"
android:label="@string/title_activity_request_accept_map"
android:screenOrientation="portrait"/>
<activity
android:name=".DropOffActivity"
android:label="Drop-Off Request"
android:screenOrientation="portrait"/>
</application>
Fatal error at runtime
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zuke.zukevalet, PID: 9243
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zuke.zukevalet/com.example.zuke.zukevalet.RequestPendingActivity}: java.lang.RuntimeException: You need to set the Android context using Firebase.setAndroidContext() before using Firebase.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.RuntimeException: You need to set the Android context using Firebase.setAndroidContext() before using Firebase.
at com.firebase.client.core.Context.getPlatform(Context.java:45)
at com.firebase.client.core.Context.ensureLogger(Context.java:218)
at com.firebase.client.core.Context.initServices(Context.java:105)
at com.firebase.client.core.Context.freeze(Context.java:92)
at com.firebase.client.core.RepoManager.getLocalRepo(RepoManager.java:55)
at com.firebase.client.core.RepoManager.getRepo(RepoManager.java:19)
at com.firebase.client.Firebase.<init>(Firebase.java:172)
at com.firebase.client.Firebase.<init>(Firebase.java:177)
at com.firebase.client.Firebase.<init>(Firebase.java:155)
at com.example.zuke.zukevalet.RequestPendingActivity.<init>(RequestPendingActivity.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
Where am I wrong?
It is in the exception message:
You need to set the Android context using Firebase.setAndroidContext() before using Firebase
Where to put Firebase.setAndroidContext() function:
To quote (step 4 of) the Firebase quickstart documentation:
The Firebase library must be initialized once with an Android
Context
. This must happen before any Firebase reference is created or used.Create
MyApplication.java
:public class MyApplication extends android.app.Application { @Override public void onCreate() { super.onCreate(); Firebase.setAndroidContext(this); } }
And update name parameter value in your
AndroidManifest.xml
:<application android:label="@string/app_name" android:name=".MyApplication"> ... </application>