Android 单独项目列表视图显示问题

Android 单独项目列表视图显示问题,android,listview,imageview,Android,Listview,Imageview,我的应用程序有问题,我正在从JSON加载时间线,没关系。现在我想单击一个项目,并在单独的活动中查看它。事情是这样的,直到我试着把一个图像 为了更好地理解: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; public class Sin

我的应用程序有问题,我正在从JSON加载时间线,没关系。现在我想单击一个项目,并在单独的活动中查看它。事情是这样的,直到我试着把一个图像

为了更好地理解:

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class SingleItemView extends Activity {
    // Declare Variables
    String country;
    String population;
    String flag;
    String position;
   ImageLoader imageLoader = new ImageLoader(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        setContentView(R.layout.singleitemview);

        Intent i = getIntent();
        // Get the result of rank
        // Get the result of country
        country = i.getStringExtra("country");
        // Get the result of population
        population = i.getStringExtra("population");
        // Get the result of flag
       flag = i.getStringExtra("flag");

        // Locate the TextViews in singleitemview.xml
        TextView txtcountry = (TextView) findViewById(R.id.country);
        TextView txtpopulation = (TextView) findViewById(R.id.population);

        // Locate the ImageView in singleitemview.xml
       ImageView imgflag = (ImageView) findViewById(R.id.flag);

        // Set results to the TextViews
        txtcountry.setText(country);
        txtpopulation.setText(population);

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
       imageLoader.DisplayImage(flag, imgflag);
    }
}
FileCache.java就是这个:

 import java.io.File;
import android.content.Context;

public class FileCache {

    private File cacheDir;

    public FileCache(Context context) {
        // Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED))
            cacheDir = new File(
                    android.os.Environment.getExternalStorageDirectory(),
                    "GettfordCommunity");
        else
            cacheDir = context.getCacheDir();
        if (!cacheDir.exists())
            cacheDir.mkdirs();
    }

    public File getFile(String url) {
        String filename = String.valueOf(url.hashCode());
        // String filename = URLEncoder.encode(url);
        File f = new File(cacheDir, filename);
        return f;

    }

    public void clear() {
        File[] files = cacheDir.listFiles();
        if (files == null)
            return;
        for (File f : files)
            f.delete();
    }

}
我已在AndroidManifest上设置了写入外部存储的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但当我点击某个项目时,应用程序崩溃:

 10-07 16:57:47.586: E/AndroidRuntime(1772): FATAL EXCEPTION: main
10-07 16:57:47.586: E/AndroidRuntime(1772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gettford.community/com.gettford.community.SingleItemView}: java.lang.NullPointerException
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.os.Looper.loop(Looper.java:137)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at java.lang.reflect.Method.invokeNative(Native Method)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at java.lang.reflect.Method.invoke(Method.java:511)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at dalvik.system.NativeStart.main(Native Method)
10-07 16:57:47.586: E/AndroidRuntime(1772): Caused by: java.lang.NullPointerException
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:200)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at com.gettford.community.FileCache.<init>(FileCache.java:18)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at com.gettford.community.ImageLoader.<init>(ImageLoader.java:35)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at com.gettford.community.SingleItemView.<init>(SingleItemView.java:15)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at java.lang.Class.newInstanceImpl(Native Method)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at java.lang.Class.newInstance(Class.java:1319)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-07 16:57:47.586: E/AndroidRuntime(1772):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-07 16:57:47.586: E/AndroidRuntime(1772):     ... 11 more
10-0716:57:47.586:E/AndroidRuntime(1772):致命异常:main
10-07 16:57:47.586:E/AndroidRuntime(1772):java.lang.RuntimeException:无法实例化活动组件信息{com.gettford.community/com.gettford.community.SingleItemView}:java.lang.NullPointerException
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread.access$600(ActivityThread.java:130)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.os.Handler.dispatchMessage(Handler.java:99)上
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.os.Looper.loop(Looper.java:137)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于java.lang.reflect.Method.Invokenactive(本机方法)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于java.lang.reflect.Method.invoke(Method.java:511)
10-07 16:57:47.586:E/AndroidRuntime(1772):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 16:57:47.586:E/AndroidRuntime(1772):在dalvik.system.NativeStart.main(本机方法)
10-07 16:57:47.586:E/AndroidRuntime(1772):由以下原因引起:java.lang.NullPointerException
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.content.ContextWrapper.getCacheDir(ContextWrapper.java:200)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于com.getford.community.FileCache.(FileCache.java:18)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于com.getford.community.ImageLoader。(ImageLoader.java:35)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于com.getford.community.SingleItemView。(SingleItemView.java:15)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于java.lang.Class.newInstanceImpl(本机方法)
10-07 16:57:47.586:E/AndroidRuntime(1772):位于java.lang.Class.newInstance(Class.java:1319)
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.Instrumentation.newActivity(Instrumentation.java:1053)上
10-07 16:57:47.586:E/AndroidRuntime(1772):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-07 16:57:47.586:E/AndroidRuntime(1772):。。。还有11个
如果我删除SingleItemView上的行以显示图像,效果会很好


提前感谢。

我发现了问题,我的设备上的AVD工作正常。只需在AVD中添加外部存储,即可使用