I am a beginner in android apps development and I'm doing this simple tutorial that make an app to add product name, price and details, after fixing some errors here and there, create products codes works in adding new products, but viewing the products information I keep getting error message : 'unfortunately app has stopped working'. I am using Eclipse Luna IDE.
In the debug page, it is shown like this :
Thread [<1> main] (Suspended(exception InflateException))
This is the java code :
AllProductsActivity.java
package com.example.test;
import java.util.ArrayList;
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.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllProductsActivity extends ListActivity {
//Progress Dialog
private ProgressDialog pDialog;
//Creating JSON parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
//url to get all products list
private static String url_all_products = "http://api.kerjaapa.com/android_connect/get_all_products.php";
//JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
//products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
//Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
//Loading products in background thread
new LoadAllProducts().execute();
//Get listview
ListView lv = getListView();
//on selecting single product
//launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
//Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
//sending pid to next activity
in.putExtra(TAG_PID, pid);
//starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
//Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//if result code 100
if (resultCode == 100) {
//if result code 100 is received
//means user edited/deleted product
//reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
//Background Async task to load all products by making http request
class LoadAllProducts extends AsyncTask<String, String, String> {
//Before starting background thread show progress dialog
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
//getting all products from url
protected String doInBackground(String... args) {
//Building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
//Check your log cat for JSON response
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
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
//Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
//creating new Hashmap
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to Hashmap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
//adding hashlist to arraylist
productsList.add(map);
}
} else {
//no products found
//launch add new product activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
//closing all previous activity
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
//after completing background task dismiss the progress dialog
protected void onPostExecute(String file_url) {
//dismiss dialog after getting products
pDialog.dismiss();
//updating UI from background thread
runOnUiThread(new Runnable() {
public void run() {
//updating parsed JSON data into listview
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
//updating listView
setListAdapter(adapter);
}
});
}
}
}
And here is for the layout xml file :
all_products.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<Textview
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<Textview
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
I really don't know where to start debugging and look for mistakes, tried using the log cat, but couldn't get anything useful to show the mistakes. Thought it might be the source problem, I have installed the latest source android SDK and attach it to the project, but under debug, there are still some file with 'source not found', these files are :
NativeStart.main(String[])line:not available[native method]
ViewRoot.handleMessage(Message)line:1727
ViewRoot.performTraversals()line:801
The stack under Thread[<1> main] are :
PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 322
SimpleAdapter.createViewFromResource(int, View, ViewGroup, int) line: 121 SimpleAdapter.getView(int, View, ViewGroup) line: 114 ListView(AbsListView).obtainView(int, boolean[]) line: 1315
ListView.measureHeightOfChildren(int, int, int, int, int) line: 1198
ListView.onMeasure(int, int) line: 1109
ListView(View).measure(int, int) line: 8171
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3132
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int) line: 1012
LinearLayout.measureVertical(int, int) line: 381
LinearLayout.onMeasure(int, int) line: 304
LinearLayout(View).measure(int, int) line: 8171
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3132
FrameLayout.onMeasure(int, int) line: 245 FrameLayout(View).measure(int, int) line: 8171
LinearLayout.measureVertical(int, int) line: 526
LinearLayout.onMeasure(int, int) line: 304
LinearLayout(View).measure(int, int) line: 8171
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int, int, int, int) line: 3132 PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 245
PhoneWindow$DecorView(View).measure(int, int) line: 8171
ViewRoot.performTraversals() line: 801
ViewRoot.handleMessage(Message) line: 1727
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: > not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Could someone help me elaborate why does these stack appear, while I think there is nothing wrong for most of them, as the source file are there (only the three files are showing 'source not found'..
I also notice that Eclipse is giving warning that NameValuePair is deprecated, could it be the problem ? and also if it is, what is the solution, I have been researching anywhere and I found to use openConnection(), but I dont'know how to use it, any guide ?
Thx again.