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

时间:2013-10-07 21:35:06

标签: 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 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

如果我删除SingleItemView上的线条以显示图像就像魅力一样。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我发现问题并且是AVD,在我的设备上工作得很完美。只需要在AVD中添加外部存储就可以了