0

我的应用程序有问题,我正在从 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 上的行以显示图像就像一个魅力。

提前致谢。

4

1 回答 1

0

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

于 2013-10-07T21:40:20.683 回答