我创建了一个应用程序,用户单击获取硬币按钮,Texview中添加了10个硬币。用户具有单独的登录界面。但是当我集成Firebase实时保存数据时,它会崩溃。
我希望每当用户单击getCoins按钮时,都应在文本字段中添加硬币,并且这些硬币也应反映在我的Firebase数据中。
Activity_Main布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar_id"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="200dp"
android:layout_height="66dp"
android:layout_gravity="center"
android:gravity="center"
android:text="GET COINS"
android:textSize="20sp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/edittext"
android:layout_width="317dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="124dp"
android:gravity="center"
android:text="COINS: 0"
android:textSize="40sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
Main Activity Class
public class MainActivity extends AppCompatActivity {
int CoinAmount = 0;
private Toolbar mainToolbar;
private FirebaseAuth mAuth;
String myStringData;
Firebase myFirebase;
TextView myEditText;
Button btngetcoin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myStringData = myEditText.getText().toString();
Firebase.setAndroidContext(this);
myFirebase = new Firebase("https://xpensive-b5178.firebaseio.com");
btngetcoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase myNewChild= myFirebase.child("PresentCoin");
}
});
btngetcoin= (Button)findViewById(R.id.btn);
myEditText = (TextView)findViewById(R.id.edittext);
final RewardedVideoAd RewardAd = MobileAds.getRewardedVideoAdInstance(this);
RewardAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
MobileAds.initialize(this);
btngetcoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(RewardAd.isLoaded()){
RewardAd.show();
}else{
RewardAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
}
});
RewardAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
}
@Override
public void onRewarded(RewardItem rewardItem) {
CoinAmount= CoinAmount +10;
myEditText.setText("COINS: "+ CoinAmount);
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
});
Logcat错误-
06-30 20:20:24.988 6074-6074/com.vikramgehlot.xpensive E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vikramgehlot.xpensive, PID: 6074
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vikramgehlot.xpensive/com.vikramgehlot.xpensive.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2687)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$1100(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.vikramgehlot.xpensive.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2640)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$1100(ActivityThread.java:186)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1609)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6016)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
答案 0 :(得分:0)
如果您要将数据保存在Firebase实时数据库中,则只需执行以下操作
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
获取数据库的引用,然后像这样保存它
mDatabase.child("Key").setValue(value);
或者您可以从此链接https://firebase.google.com/docs/database/android/read-and-write
获取参考答案 1 :(得分:0)
未使用对您具有的按钮对象的引用来初始化变量btngetcoin。将以下findViewById()代码移至您设置的setOnClickListener上方,
myFirebase = new Firebase("https://xpensive-b5178.firebaseio.com");
// you need to initialize this before attaching listener!!
btngetcoin= (Button)findViewById(R.id.btn);
btngetcoin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Firebase myNewChild= myFirebase.child("PresentCoin");
}
});