无法实例化活动组件信息(Volley)

时间:2014-07-05 10:56:15

标签: java android android-volley

我想实施Android排球,所以请按照以下示例1 2

项目编译没有错误但是没有触发HTTP请求并且android抛出了这个错误:

07-05 06:33:09.596: E/AndroidRuntime(1943): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.testsite.testvolley/com.testsite.testvolley.MainActivity}: java.lang.NullPointerException

MainActivity:

package com.testsite.testvolley;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.testsite.testvolley.MyVolley;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestQueue queue = MyVolley.getRequestQueue();
        StringRequest myReq = new StringRequest(Method.GET, 
                                                "http://www.google.com/",
                                                createMyReqSuccessListener(),
                                                createMyReqErrorListener());

        queue.add(myReq);

    }

    TextView tv = (TextView) findViewById(R.id.textView1);

    private Response.Listener<String> createMyReqSuccessListener() {
        return new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                tv.setText(response);
            }
        };
    }


    private Response.ErrorListener createMyReqErrorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                tv.setText(error.getMessage());
            }
        };
    }

}

MyVolley.java:

package com.testsite.testvolley;

import android.content.Context;
import android.app.ActivityManager;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.testsite.testvolley.BitmapLruCache;

public class MyVolley {
    private static RequestQueue mRequestQueue;
    private static ImageLoader mImageLoader;


    private MyVolley() {
        // no instances
    }


    static void init(Context context) {
        mRequestQueue = Volley.newRequestQueue(context);

        int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                .getMemoryClass();
        // Use 1/8th of the available memory for this memory cache.
        int cacheSize = 1024 * 1024 * memClass / 8;
        mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize));
    }


    public static RequestQueue getRequestQueue() {
        if (mRequestQueue != null) {
            return mRequestQueue;
        } else {
            throw new IllegalStateException("RequestQueue not initialized");
        }
    }

    public static ImageLoader getImageLoader() {
        if (mImageLoader != null) {
            return mImageLoader;
        } else {
            throw new IllegalStateException("ImageLoader not initialized");
        }
    }
}

BitmapLruCache.java:

package com.testsite.testvolley;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
    public BitmapLruCache(int maxSize) {
        super(maxSize);
    }


    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight();
    }


    @Override
    public Bitmap getBitmap(String url) {
        return get(url);
    }


    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }
}

Android Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testsite.testvolley"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.testsite.testvolley.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.testsite.testvolley.MyVolley"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name="com.testsite.testvolley.BitmapLruCache"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

我检查了volley.jar文件也包含在Buildpath中。任何人都可以指出我仍然缺少什么?

编辑:LogCat:

    07-05 07:25:06.755: E/AndroidRuntime(1933): FATAL EXCEPTION: main
07-05 07:25:06.755: E/AndroidRuntime(1933): Process: com.testsite.testvolley, PID: 1933
07-05 07:25:06.755: E/AndroidRuntime(1933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testsite.testvolley/com.testsite.testvolley.MainActivity}: java.lang.IllegalStateException: RequestQueue not initialized
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.os.Looper.loop(Looper.java:136)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invoke(Method.java:515)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at dalvik.system.NativeStart.main(Native Method)
07-05 07:25:06.755: E/AndroidRuntime(1933): Caused by: java.lang.IllegalStateException: RequestQueue not initialized
07-05 07:25:06.755: E/AndroidRuntime(1933):     at com.testsite.testvolley.MyVolley.getRequestQueue(MyVolley.java:36)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at com.testsite.testvolley.MainActivity.onCreate(MainActivity.java:24)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.Activity.performCreate(Activity.java:5231)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-05 07:25:06.755: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-05 07:25:06.755: E/AndroidRuntime(1933):     ... 11 more

2 个答案:

答案 0 :(得分:1)

我也面临同样的问题...... 但我通过使用另一个扩展Application的类(App_VolleyExample.java)来修复它 ...

public class App_VolleyExamples extends Application {

    private static Context applicationContext;
    @Override
    public void onCreate() {
        super.onCreate();

        applicationContext = this.getApplicationContext();
        MyVolley.init(applicationContext);

    }

最重要的是......你必须在清单(在应用程序下)声明

<application
        android:name="com.xxx.App_VolleyExamples"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
    .....
</application>

在我的情况下,我这样解决了。希望这有帮助。

答案 1 :(得分:0)

问题是您在findViewById方法之外设置textview onCreate。因此,在活动方法中初始化textview不在活动类中。 即改变

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestQueue queue = MyVolley.getRequestQueue();
        StringRequest myReq = new StringRequest(Method.GET, 
                                                "http://www.google.com/",
                                                createMyReqSuccessListener(),
                                                createMyReqErrorListener());

        queue.add(myReq);

    }

    TextView tv = (TextView) findViewById(R.id.textView1);

public class MainActivity extends Activity {
      TextView tv; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);

RequestQueue queue = MyVolley.getRequestQueue(this);
        StringRequest myReq = new StringRequest(Method.GET, 
                                                "http://www.google.com/",
                                                createMyReqSuccessListener(),
                                                createMyReqErrorListener());

        queue.add(myReq);


    }

修改

您没有初始化齐射的请求队列,因此首先将其初始化。 所以改变这个

  public static RequestQueue getRequestQueue() {
        if (mRequestQueue != null) {
            return mRequestQueue;
        } else {
            throw new IllegalStateException("RequestQueue not initialized");
        }
    }

public static RequestQueue getRequestQueue(Context mContext) {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(mContext);  
    } 
    return mRequestQueue;
}

并更改

    RequestQueue queue = MyVolley.getRequestQueue();

RequestQueue queue = MyVolley.getRequestQueue(this);

OnCreate活动方法中。