Android线程不断被挂起(异常InflateException))

时间:2022-03-24 21:46:59

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.

我是Android应用程序开发的初学者,我正在做这个简单的教程,使应用程序添加产品名称,价格和细节,在修复一些错误之后,创建产品代码工作添加新产品,但查看产品信息我不断收到错误消息:'不幸的是应用程序已停止工作'。我正在使用Eclipse Luna IDE。

In the debug page, it is shown like this :

在调试页面中,它显示如下:

Thread [<1> main] (Suspended(exception InflateException))

线程[<1> main](暂停(异常InflateException))

This is the java code :

这是java代码:

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 :

这里是布局xml文件:

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 :

我真的不知道从哪里开始调试并查找错误,尝试使用日志猫,但无法获得任何有用的显示错误。认为它可能是源问题,我已经安装了最新的源android SDK并将其附加到项目中,但是在调试下,仍然有一些文件“找不到源”,这些文件是:

NativeStart.main(String[])line:not available[native method]

NativeStart.main(String [])行:不可用[native method]

ViewRoot.handleMessage(Message)line:1727

ViewRoot.performTraversals()line:801

The stack under Thread[<1> main] are :

Thread [<1> main]下的堆栈是:

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]

PhoneLayoutInflater(LayoutInflater).inflate(int,ViewGroup,boolean)行:322 SimpleAdapter.createViewFromResource(int,View,ViewGroup,int)行:121 SimpleAdapter.getView(int,View,ViewGroup)行:114 ListView(AbsListView).obtainView (int,boolean [])行:1315 ListView.measureHeightOfChildren(int,int,int,int,int)行:1198 ListView.onMeasure(int,int)行:1109 ListView(View).measure(int,int)行:8171 LinearLayout(ViewGroup).measureChildWithMargins(View,int,int,int,int)行:3132 LinearLayout.measureChildBeforeLayout(View,int,int,int,int,int)行:1012 LinearLayout.measureVertical(int,int)行:381 LinearLayout.onMeasure(int,int)行:304 LinearLayout(View).measure(int,int)行:8171 FrameLayout(ViewGroup).measureChildWithMargins(View,int,int,int,int)行:3132 FrameLayout.onMeasure (int,int)行:245 FrameLayout(View).measure(int,int)行:8171 LinearLayout.measureVertical(int,int)行:526 LinearLayout.onMeasure(int,int)行:304 LinearLayo ut(View).measure(int,int)行:8171 PhoneWindow $ DecorView(ViewGroup).measureChildWithMargins(View,int,int,int,int)行:3132 PhoneWindow $ DecorView(FrameLayout).onMeasure(int,int)行:245 PhoneWindow $ DecorView(View).measure(int,int)行:8171 ViewRoot.performTraversals()行:801 ViewRoot.handleMessage(消息)行:1727 ViewRoot(Handler).dispatchMessage(消息)行:99 Looper.loop ()行:123 ActivityThread.main(String [])line:4627 Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)line:> not available [native method] Method.invoke (Object,Object ...)行:521 ZygoteInit $ MethodAndArgsCaller.run()行:868 ZygoteInit.main(String [])行:626 NativeStart.main(String [])行:不可用[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 ?

我还注意到Eclipse正在发出警告,NameValuePair已被弃用,这可能是问题吗?如果它是,解决方案是什么,我一直在研究任何地方,我发现使用openConnection(),但我不知道如何使用它,任何指南?

Thx again.

1 个解决方案

#1


0  

Try making the "V" in "Textview" capital in the two instances in list_item.xml (so it will read TextView instead). I think the problem has to do with it not being able to inflate a those, since that's not the class name.

尝试在list_item.xml中的两个实例中的“Textview”中创建“V”(因此它将读取TextView)。我认为问题与它无法使那些人膨胀有关,因为那不是班级名称。

#1


0  

Try making the "V" in "Textview" capital in the two instances in list_item.xml (so it will read TextView instead). I think the problem has to do with it not being able to inflate a those, since that's not the class name.

尝试在list_item.xml中的两个实例中的“Textview”中创建“V”(因此它将读取TextView)。我认为问题与它无法使那些人膨胀有关,因为那不是班级名称。