崩溃编译此代码。
这是HOME.java
public class Home extends ActionBarActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> empresaList;
// url to get all products list
private static String url_all_empresas = "http://my_url/get_all_empresas.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "empresas";
//private static final String TAG_ID = "id";
private static final String TAG_NOMBRE = "nombre";
private static final String TAG_TITULAR = "titular";
private static final String TAG_DIRECCION = "direccion";
private static final String TAG_LOCALIDAD = "localidad";
private static final String TAG_TELEFONO = "telefono";
// products JSONArray
JSONArray products = null;
ListView lista;
private AdView mAdView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setContentView(R.layout.dia_siguiente);
AdView mAdView = (AdView) findViewById(R.id.ad_view);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Hashmap para el ListView
empresaList = new ArrayList<HashMap<String, String>>();
// Cargar los productos en el Background Thread
new LoadAllProducts().execute();
lista = (ListView) findViewById(R.id.listAllProducts);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}//fin onCreate
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Antes de empezar el background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Home.this);
pDialog.setMessage("Cargando... Por favor espere...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* obteniendo todos los productos
* */
protected String doInBackground(String... args) {
// Building Parameters
List params = new ArrayList();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_empresas, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
//Log.i("ramiro", "produtos.length" + products.length());
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
// String id = c.getString(TAG_ID);
String name = c.getString(TAG_NOMBRE);
String titular = c.getString(TAG_TITULAR);
String direccion = c.getString(TAG_DIRECCION);
String localidad = c.getString(TAG_LOCALIDAD);
String telefono = c.getString(TAG_TELEFONO);
// creating new HashMap
HashMap map = new HashMap();
// adding each child node to HashMap key => value
// map.put(TAG_ID, id);
map.put(TAG_NOMBRE, name);
map.put(TAG_TITULAR, titular);
map.put(TAG_DIRECCION, direccion);
map.put(TAG_LOCALIDAD, localidad);
map.put(TAG_TELEFONO, telefono);
empresaList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Home.this,
empresaList,
R.layout.single_post,
new String[] {
// TAG_ID,
TAG_NOMBRE,
TAG_TITULAR,
TAG_DIRECCION,
TAG_LOCALIDAD,
TAG_TELEFONO,
},
new int[] {
//R.id.single_post_tv_id,
R.id.single_post_tv_nombre,
R.id.single_post_tv_titular,
R.id.single_post_tv_direccion,
R.id.single_post_tv_localidad,
R.id.single_post_tv_telefono,
});
// updating listview
//setListAdapter(adapter);
lista.setAdapter(adapter);
}
});
}
}
}
尝试运行时出现以下错误:
********************LOGCAT***********************************************
08-18 01:23:13.904 8565-8565/com.disemur.farmaguar E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.disemur.farmaguar, PID: 8565
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.disemur.farmaguar.Home$LoadAllProducts$1.run(Home.java:211)
at android.app.Activity.runOnUiThread(Activity.java:5575)
at com.disemur.farmaguar.Home$LoadAllProducts.onPostExecute(Home.java:185)
at com.disemur.farmaguar.Home$LoadAllProducts.onPostExecute(Home.java:106)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
答案 0 :(得分:0)
此声明很可能没有按预期执行。
lista = (ListView) findViewById(R.id.listAllProducts);
使用调试器或日志检查结果。
答案 1 :(得分:0)
发布R.layout.activity_home
代码
我怀疑你提供的listview ID不正确
您已在活动的setContentView
方法上设置了两次视图onCreate()
。
找出哪个布局正确,并且应该在当前膨胀的布局中定义列表视图。
在onCreate()
方法