无法使用JSON解析器将图像从WebPage显示到android

时间:2014-02-04 06:08:14

标签: java android json parsing imageview

我是android的新手,并且正在使用JSON解析我的网页数据的webservices,关注androidhive json解析器教程..现在能够解析标题而不是图像,用logcat检查但是甚至没有错误。感觉很难找到没有错误的问题,帮助将得到赞赏,非常感谢

public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TITLE = "title";
static String ALTERNATE = "alternate";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Android JSON Parse Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("https://www.facebook.com/feeds/page.php?format=json&id=APP_ID");


        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("entries");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("title", jsonobject.getString("title"));
                map.put("alternate", jsonobject.getString("alternate"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Set the adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

SingleListview.activity

public class SingleItemView extends Activity {
// Declare Variables
String title;   
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
    title = i.getStringExtra("title");      
    alternate = i.getStringExtra("alternate");

    // Locate the TextViews in singleitemview.xml
    TextView txttitle = (TextView) findViewById(R.id.rank);
    // Locate the ImageView in singleitemview.xml
    ImageView imgalternate = (ImageView) findViewById(R.id.flag);

    // Set results to the TextViews
    txttitle.setText(title);        
    imageLoader.DisplayImage(alternate, imgalternate);
}

但是当点击ImageView获取NullPointer异常时。 Logcat输出

 02-04 11:47:28.078: E/AndroidRuntime(706): java.lang.RuntimeException: Unable to  instantiate activity   ComponentInfo{com.androidbegin.jsonparsetutorial/com.androidbegin.jsonparsetutorial.SingleItemView}: java.lang.NullPointerException
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.os.Handler.dispatchMessage(Handler.java:99)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.os.Looper.loop(Looper.java:123)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at java.lang.reflect.Method.invokeNative(Native Method)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at java.lang.reflect.Method.invoke(Method.java:507)
  02-04 11:47:28.078: E/AndroidRuntime(706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  02-04 11:47:28.078: E/AndroidRuntime(706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
   02-04 11:47:28.078: E/AndroidRuntime(706):   at dalvik.system.NativeStart.main(Native Method)
 02-04 11:47:28.078: E/AndroidRuntime(706): Caused by: java.lang.NullPointerException
 02-04 11:47:28.078: E/AndroidRuntime(706):     at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:188)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at com.androidbegin.jsonparsetutorial.FileCache.<init>(FileCache.java:18)
  02-04 11:47:28.078: E/AndroidRuntime(706):    at  com.androidbegin.jsonparsetutorial.ImageLoader.<init>(ImageLoader.java:34)
  02-04 11:47:28.078: E/AndroidRuntime(706):    at com.androidbegin.jsonparsetutorial.SingleItemView.<init>(SingleItemView.java:17)
  02-04 11:47:28.078: E/AndroidRuntime(706):    at java.lang.Class.newInstanceImpl(Native Method)
 02-04 11:47:28.078: E/AndroidRuntime(706):     at java.lang.Class.newInstance(Class.java:1409)
02-04 11:47:28.078: E/AndroidRuntime(706):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-04 11:47:28.078: E/AndroidRuntime(706):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-04 11:47:28.078: E/AndroidRuntime(706):  ... 11 more

2 个答案:

答案 0 :(得分:1)

如果您尝试检索较大的图片,请使用Namevaluepair代替JSON。如果图像大约为50kb,则可以使用JSON

答案 1 :(得分:0)

  

我认为你完全错了。

首先,您的ListView将不是正常ListView,而是自定义/自定义。

为此,您需要准备单独的Adapter类,以扩展任何适配器(BaseadapterArrayAdapter)。

请参阅此链接:Android Custom ListView with Image and Text