尝试调用虚方法getApplicationContext()'在片段活动的空对象引用上

时间:2016-12-20 15:55:27

标签: android android-fragments

好。我有一个扩展Fragment的Class是该类的内容 的 Category.java

public class Category extends Fragment {

private TextView cat_txt1;
private TextView cat_txt2;
private TextView cat_txt3;
private TextView cat_txt4;
private TextView cat_txt5;
private TextView cat_txt6;
private TextView cat_txt7;
private TextView cat_txt8;
private TextView cat_txt9;
private TextView cat_txt10;
private  AppController app_controller;
public static FAQ newInstance(String title) {
    FAQ fragmentFirst = new FAQ();
    /*Bundle args = new Bundle();
    args.putInt("someInt", page);
    args.putString("someTitle", title);
    fragmentFirst.setArguments(args);*/
    return fragmentFirst;
}
//TextView TvFAQ;
// Store instance variables based on arguments passed

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    app_controller=new AppController();
    Toast.makeText(getActivity(), "inflated",
            Toast.LENGTH_LONG).show();
    View view = inflater.inflate(R.layout.fragment_category, container, false);
    cat_txt1=(TextView) view.findViewById(R.id.txt_cat1);
    cat_txt2=(TextView) view.findViewById(R.id.txt_cat2);
    cat_txt3=(TextView) view.findViewById(R.id.txt_cat3);
    cat_txt4=(TextView) view.findViewById(R.id.txt_cat4);
    cat_txt5=(TextView) view.findViewById(R.id.txt_cat5);
    cat_txt6=(TextView) view.findViewById(R.id.txt_cat6);
    cat_txt7=(TextView) view.findViewById(R.id.txt_cat7);
    cat_txt8=(TextView) view.findViewById(R.id.txt_cat8);
    cat_txt9=(TextView) view.findViewById(R.id.txt_cat9);
    cat_txt10=(TextView) view.findViewById(R.id.txt_cat10);
    load_categories(MetaInfo.getMetadata(getActivity(),"yapplication_id"));
    return view;
}


private void load_categories(final String application_id){

    // Tag used to cancel the request
    String tag_json_obj = "json_obj_req";

    Toast.makeText(getActivity(), application_id,
            Toast.LENGTH_LONG).show();
    String url =  getResources().getString(R.string.ybase_path)+"getjson.php";

    ///ProgressDialog pDialog = new ProgressDialog(this);
    //pDialog.setMessage("Loading...");
    //pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d("rewangojson", response.toString());
                    //pDialog.hide();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("json err", "Error: " + error.getMessage());
          //  pDialog.hide();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("application_id", application_id);
            return params;
        }

    };

// Adding request to request queue
    app_controller.addToRequestQueue(jsonObjReq, tag_json_obj);
}

}

我正在使用以下代码

在我的Dashboard Activity中动态添加上述类
FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Category category = new Category();
    fragmentTransaction.add(R.id.dashboard_fragment_conatiner, category, "Category");
    fragmentTransaction.commit();

我在Category.java上调用函数addToRequestQueue()。该函数存在于类名AppController中。以下是AppController类中的代码。我在AppController类中得到错误。

public class AppController extends Application {

public static final String TAG = AppController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    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);
    }
}
}

以下是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.if">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:name=".helpers.classes.ConnectivityListenerSet"

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity
        android:name=".Dashboard"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".Entry"
        android:label="@string/title_activity_entry"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />

    <receiver
        android:name=".helpers.classes.ConnectivityReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>


    <activity
        android:name=".TabbedContent"
        android:label="@string/about"
        android:theme="@style/NoAB"
        ></activity>
</application>

</manifest>

0 个答案:

没有答案