私はvolleyライブラリを使用しているプロジェクトを持っています。 私はMyApplication
クラスを持っている:Androidライブラリでvolleyを使用する方法
public class MyApplication extends Application {
public static final String TAG = MyApplication.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
今私のプロジェクトでは、私のManifest
ファイル内のクラス名を追加していことを使用します。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".MyApplication"
>
すべて正常です。
主な質問になると、私はボレーを使用する必要がある図書館も作っています。 私のライブラリでは、MyApplication
クラスを作成し、マニフェストにアプリケーション名を追加しました。私はまた、マニフェストにインターネット許可を追加しました。
私の関数を呼び出すとき、それは私にnull参照エラーを与えています。
Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference
私の質問:
A)私たちは、図書館でボレーのを使用することができます。右?
b)「はい」の場合、エラーが表示されるのはなぜですか?いいえの場合は、サーバーとどのように対話しますか?
私はmavenリポジトリを作成する予定ですので、メインプロジェクトファイルの変更はできません。
プロジェクトのマニフェストでアプリケーション名をlibrarieのMyApplication
クラスに変更しようとしました。その場合、サーバーに接続することができました。
しかし、ユーザーが自分のカスタムクラスのアプリケーション名を既に変更している場合は、どうすれば目標を達成できますか?
シングルトンクラスも試しましたが、同じエラーが表示されています。
おかげ
ライブラリは 'Application'クラスを使用するはずですか?あなたはVolleyを使うのにそれを必要とせず、私はあなたが図書館でVolleyを使うことができると確信しています。 –
addメソッドの前にgetRequestQueueを呼び出していますか? beacuseはRequestQueueのインスタンスを作成する場所です。 – Pablo
@ cricket_007なので、プロジェクトには1つのアプリケーションクラスしかないと言ってもいいですか?ライブラリには独自のアプリケーションクラスはありませんか? – driftking9987