我正在使用名为“AplikasiLokomedia”的应用程序在eclipse上收到错误消息。它显示错误信息不幸的是AplikasiLokomedia已停止:
这是一个错误日志
03-28 07:27:39.253: E/JSON Parser(1330): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
03-28 07:27:39.373: W/dalvikvm(1330): threadid=11: thread exiting with uncaught exception (group=0xb3adeb90)
03-28 07:27:39.543: E/AndroidRuntime(1330): FATAL EXCEPTION: AsyncTask #1
03-28 07:27:39.543: E/AndroidRuntime(1330): Process: com.dharma.aplikasilokomedia, PID: 1330
03-28 07:27:39.543: E/AndroidRuntime(1330): java.lang.RuntimeException: An error occured while executing doInBackground()
03-28 07:27:39.543: E/AndroidRuntime(1330): at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-28 07:27:39.543: E/AndroidRuntime(1330): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.lang.Thread.run(Thread.java:841)
03-28 07:27:39.543: E/AndroidRuntime(1330): Caused by: java.lang.NullPointerException
03-28 07:27:39.543: E/AndroidRuntime(1330): at com.dharma.aplikasilokomedia.LokoUtama$AmbilData.doInBackground(LokoUtama.java:106)
03-28 07:27:39.543: E/AndroidRuntime(1330): at com.dharma.aplikasilokomedia.LokoUtama$AmbilData.doInBackground(LokoUtama.java:1)
03-28 07:27:39.543: E/AndroidRuntime(1330): at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-28 07:27:39.543: E/AndroidRuntime(1330): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-28 07:27:39.543: E/AndroidRuntime(1330): ... 4 more
03-28 07:27:40.263: I/Choreographer(1330): Skipped 86 frames! The application may be doing too much work on its main thread.
03-28 07:27:40.643: I/Choreographer(1330): Skipped 96 frames! The application may be doing too much work on its main thread.
03-28 07:27:41.413: I/Choreographer(1330): Skipped 105 frames! The application may be doing too much work on its main thread.
03-28 07:27:41.693: I/Choreographer(1330): Skipped 44 frames! The application may be doing too much work on its main thread.
03-28 07:27:42.143: I/Choreographer(1330): Skipped 64 frames! The application may be doing too much work on its main thread.
03-28 07:27:43.233: I/Choreographer(1330): Skipped 31 frames! The application may be doing too much work on its main thread.
03-28 07:27:43.533: I/Choreographer(1330): Skipped 52 frames! The application may be doing too much work on its main thread.
03-28 07:27:44.973: E/WindowManager(1330): android.view.WindowLeaked: Activity com.dharma.aplikasilokomedia.LokoUtama has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b3d91a80 V.E..... R.....ID 0,0-456,144} that was originally added here
03-28 07:27:44.973: E/WindowManager(1330): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
03-28 07:27:44.973: E/WindowManager(1330): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
03-28 07:27:44.973: E/WindowManager(1330): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.Dialog.show(Dialog.java:286)
03-28 07:27:44.973: E/WindowManager(1330): at com.dharma.aplikasilokomedia.LokoUtama$AmbilData.onPreExecute(LokoUtama.java:94)
03-28 07:27:44.973: E/WindowManager(1330): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
03-28 07:27:44.973: E/WindowManager(1330): at android.os.AsyncTask.execute(AsyncTask.java:535)
03-28 07:27:44.973: E/WindowManager(1330): at com.dharma.aplikasilokomedia.LokoUtama.onCreate(LokoUtama.java:57)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.Activity.performCreate(Activity.java:5243)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-28 07:27:44.973: E/WindowManager(1330): at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 07:27:44.973: E/WindowManager(1330): at android.os.Looper.loop(Looper.java:137)
03-28 07:27:44.973: E/WindowManager(1330): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 07:27:44.973: E/WindowManager(1330): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 07:27:44.973: E/WindowManager(1330): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 07:27:44.973: E/WindowManager(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 07:27:44.973: E/WindowManager(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 07:27:44.973: E/WindowManager(1330): at dalvik.system.NativeStart.main(Native Method)
这是我的JSONParser.JAVA
package com.dharma.aplikasilokomedia;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是我的DetailLoko.java
package com.dharma.aplikasilokomedia;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailLoko extends Activity {
public ImageLoader imageLoader;
{
imageLoader = new ImageLoader(null);
}
JSONArray string_json = null;
String idberita;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
public static final String TAG_ID = "id";
public static final String TAG_JUDUL = "judul";
public static final String TAG_GAMBAR = "gambar";
private static final String url_detail_berita = "http://10.0.2.2/lokomedia/lokoandro/detailberita.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
Intent i = getIntent();
idberita = i.getStringExtra(TAG_ID);
new AmbilDetailBerita().execute();
}
class AmbilDetailBerita extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DetailLoko.this);
pDialog.setMessage("Mohon Tunggu ... !");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... params) {
try {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("idberita",idberita));
JSONObject json = jsonParser.makeHttpRequest(
url_detail_berita, "GET", params1);
string_json = json.getJSONArray("berita");
runOnUiThread(new Runnable() {
public void run() {
ImageView thumb_image = (ImageView) findViewById(R.id.imageView1);
TextView judul = (TextView) findViewById(R.id.judul);
TextView detail = (TextView) findViewById(R.id.detail);
TextView isi = (TextView) findViewById(R.id.content);
try {
// ambil objek member pertama dari JSON Array
JSONObject ar = string_json.getJSONObject(0);
String judul_d = ar.getString("judul");
String detail_d = ar.getString("hari")+" , "+ar.getString("tanggal")+" Diposting Oleh : "+ar.getString("username");
String isi_d = ar.getString("isi");
judul.setText(judul_d);
detail.setText(detail_d);
isi.setText(isi_d);
imageLoader.DisplayImage(ar.getString(TAG_GAMBAR),thumb_image);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.home:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
finish();
Intent i = new Intent(getApplicationContext(), LokoUtama.class);
startActivity(i);
return true;
case R.id.exit:
keluar();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void keluar(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah Anda Ingin" + " keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
}
这是我的LokoUtama.JAVA
package com.dharma.aplikasilokomedia;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class LokoUtama extends Activity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> DaftarBerita = new ArrayList<HashMap<String, String>>();
private static String url_berita = "http://10.0.2.2/lokomedia/lokoandro/berita.php";
public static final String TAG_ID = "id";
public static final String TAG_JUDUL = "judul";
public static final String TAG_GAMBAR = "gambar";
JSONArray string_json = null;
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loko_utama);
DaftarBerita = new ArrayList<HashMap<String, String>>();
new AmbilData().execute();
list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = DaftarBerita.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), DetailLoko.class);
in.putExtra(TAG_ID, map.get(TAG_ID));
in.putExtra(TAG_GAMBAR, map.get(TAG_GAMBAR));
startActivity(in);
}
});
}
public void SetListViewAdapter(ArrayList<HashMap<String, String>> berita) {
adapter = new LazyAdapter(this, berita);
list.setAdapter(adapter);
}
class AmbilData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LokoUtama.this);
pDialog.setMessage("Mohon tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_berita, "GET",params);
try {
string_json = json.getJSONArray("berita");
for (int i = 0; i < string_json.length(); i++) {
JSONObject c = string_json.getJSONObject(i);
String id_berita = c.getString(TAG_ID);
String judul = c.getString(TAG_JUDUL);
String link_image = c.getString(TAG_GAMBAR);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id_berita);
map.put(TAG_JUDUL, judul);
map.put(TAG_GAMBAR, link_image);
DaftarBerita.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
SetListViewAdapter(DaftarBerita);
//Update Time..
// Current Date
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedDate = df.format(c.getTime());
TextView updateTime = (TextView) findViewById(R.id.update);
updateTime.setText("Terakhir di Update : " + formattedDate);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loko_utama, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.refresh:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
finish();
startActivity(getIntent());
Toast.makeText(LokoUtama.this, "Update Data Berita", Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
keluar();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void keluar(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah Anda Ingin" + " keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
}
这是ImageLoader.JAVA的代码
package com.dharma.aplikasilokomedia;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.no_image;
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null)
return b;
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
我有其他文件名 1. FileCache.JAVA 2. LazyAdafter.JAVA 3. MemoryCache.JAVA 4. Utils.JAVA
谢谢,如果有人愿意帮我解决问题......
答案 0 :(得分:0)
它不是NullPointerException,而是JSONException。 您正在尝试解析像JSON
这样的XML文档"Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject"
答案 1 :(得分:0)
我很吵,我还不能发表评论,但是@Raghunandan询问了第106行:
string_json = json.getJSONArray("berita");
所以我假设,正如其他人在我之前所做的那样:json为空。
嗯,等等。您的第一篇文章中是否缺少这些Parser-Error行?那么你有问题......