2016-07-27 4 views
0

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(); 
     } 
    } 
} 

にそれを得るが、それは私にエラーが発生します。

答えて

1

私は、値をFirebaseInstanceIdServiceに渡すことは悪いアプローチであると考えています。

なぜかSharedPreferencesで遊ぶのはなぜですか?

トークンが更新されると、単に以下のように嗜好のトークンを保存を:

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 
    private static final String TAG = "MyFirebaseIIDService"; 

    @Override 
    public void onTokenRefresh() { 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

     sendRegistrationToServer(refreshedToken); 
    } 

    private void sendRegistrationToServer(String token) { 
     SharedPreferenceUtils.getInstance(this).setValue(getString(R.string.firebase_cloud_messaging_token), token); 
    } 
} 

その後、私は以下のように環境設定から、そのトークンを取得するために私BaseActivityでメソッドを作成しました:

public String getDeviceToken() { 
    return SharedPreferenceUtils 
      .getInstance(this) 
      .getStringValue(getString(R.string.firebase_cloud_messaging_token), "").equals("") 
      ? FirebaseInstanceId.getInstance().getToken() 
      : SharedPreferenceUtils.getInstance(this).getStringValue(getString(R.string.firebase_cloud_messaging_token), ""); 
} 

このようにして、私は新しく生成されたトークンをプリファレンスに格納し、後でそれを使用するためにアクセスするグローバルなポイントを作成しました。

このようにすることができます。

編集:私たちがSharedPreferencesで簡単に作業できるように作成したユーティリティクラスです。

Github Gistから入手できます。

+0

こんにちは、チンタン、ご協力いただきありがとうございます。私はアンドロイドに新しいです。私のコードを構築するためにmoを助けることができますか?私は私のコードを更新..ありがとう..私はあなたのコードを試みたが、私はSharedPreferenceUtilsでエラーを取得しています。 – Joehamir

+0

@Joehamirが回答を更新しました。チェックしてください。 –

+0

シンボルfirebase_cloud_messaging_tokenを解決できません – Joehamir

0

FirebaseSetGetクラスのオブジェクトが

FirebaseSetGet idsvar = new FirebaseSetGet(); 

今あなたがFirebaseSetGetクラスのメソッドを呼び出すことができ、それを初期化する最初に初期化されていません。

+0

はsumitと一致します –

+0

は動作しませんMainActivityからFirebaseInstanceIdServiceに値を渡すことは可能ですか? – Joehamir

+0

をご覧ください。 FirebaseInstanceIDServiceコードを更新します。 – Joehamir

関連する問題