MainActivityから値を設定してFirebaseInstanceクラスに取得しようとしました。Android StudioのFirebaseインスタンスへの値の取得と設定
が、私は
Process: com.administrator.mosbeau, PID: 17713
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.administrator.mosbeau/com.example.administrator.mosbeau.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.administrator.mosbeau.FirebaseSetGet.setCountryid(java.lang.String)' on a null object reference
は、ここで私がやったことだ、このエラーが発生します。
私はその後、私は私のMainActivityにこのコードを追加しようとしました
public class FirebaseSetGet extends Application {
private String countryid;
private String customerid;
public String getCountryid() {
return countryid;
}
public void setCountryid(String acountryid) {
countryid = acountryid;
}
public String getCustomerid() {
return customerid;
}
public void setCustomerid(String acustomerid) {
customerid = acustomerid;
}
}
をFirebaseSetGetクラスを作成しました。
private void displayUserDetails() {
User user = userLocalStore.getLoggedInUser();
customersid = user.customers_id;
countrycode = user.customers_countryid;
stateid = user.customers_stateid;
if (customersid == "") {
Intent myIntent = new Intent(MainActivity.this, IndexActivity.class);
startActivity(myIntent);
}
if (checkPlayServices()) {
idsvar.setCountryid(countrycode);
idsvar.setCustomerid(customersid);
FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
if (productId != null){
if (productFragment != null) {
if (productFragment.equals("productItem")) {
ProductFragment productfragment = ProductFragment.newInstance(productId, productName, countrycode, customersid, categoryName, stateid, categoryId);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, productfragment)
.addToBackStack(null)
.commit();
}
}
}
}
その後、私のFirebaseInstance
public class FirebaseInstanceIDService extends FirebaseInstanceIdService {
FirebaseSetGet idsvar = new FirebaseSetGet();
@Override
public void onTokenRefresh() {
String token = FirebaseInstanceId.getInstance().getToken();
final String country_id = idsvar.getCountryid();
final String customer_id = idsvar.getCustomerid();
registerToken(token,country_id,customer_id);
}
private void registerToken(String token, String country_id, String customer_id) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("Token",token)
.add("Countryid",country_id)
.add("Customerid",customer_id)
.build();
Request request = new Request.Builder()
.url("http://shop.mosbeau.com.ph/FCM/register.php")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
にそれを得るが、それは私にエラーが発生します。
こんにちは、チンタン、ご協力いただきありがとうございます。私はアンドロイドに新しいです。私のコードを構築するためにmoを助けることができますか?私は私のコードを更新..ありがとう..私はあなたのコードを試みたが、私はSharedPreferenceUtilsでエラーを取得しています。 – Joehamir
@Joehamirが回答を更新しました。チェックしてください。 –
シンボルfirebase_cloud_messaging_tokenを解決できません – Joehamir