如何清除android中的膨胀错误?

时间:2017-01-05 08:31:50

标签: android android-layout

您好我正在创建一个布局,并在其中显示ListView以显示5列中的小图标。我正在使用AysncTask来解析JSON以获取我需要在列表视图中显示的图像详细信息。现在我收到了错误。我不知道如何解决。请帮我解决。

我的布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_seatvisible"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.notebook.runtowin.Activity.Seatvisible">


        <TextView
            android:id="@+id/from_to"
            android:layout_width="match_parent"
            android:layout_height="35sp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimaryDark"
            android:text="Select your seat"
            android:padding="5sp"
            android:gravity="center_vertical"
            android:textStyle="bold"
            android:textColor="@color/colorwhite" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/from_to">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FFEB3B"
            android:id="@+id/headinglayout">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/busname"
                    android:padding="5sp"
                    android:text="RUN TO WIN"
                    android:textStyle="bold"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_below="@+id/busname"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/seatnumber"
                    android:padding="5sp"
                    android:text="Seats :"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@+id/seatnumber"
                    android:text="25,24"
                    android:id="@+id/seatsnumber"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ticketprice"
                android:layout_alignParentRight="true"
                android:text="hsdshd"
                android:textStyle="bold"
                android:padding="5sp"
                android:textSize="30sp"
                android:layout_marginRight="10sp"/>

        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_below="@+id/headinglayout"
            android:layout_marginTop="3sp"
            android:paddingTop="10sp"
            android:paddingRight="25sp"
            android:paddingLeft="25dp">

            <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:background="@drawable/iconsteer"
                android:id="@+id/imageView2"
                android:layout_marginEnd="50sp"
                android:layout_alignParentEnd="true"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               android:layout_below="@+id/imageView2"
                android:layout_marginTop="20sp"
                android:id="@+id/listviewseat">

            </ListView>


        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="25sp"
            android:layout_alignParentEnd="true"
            android:gravity="center_horizontal"
            android:background="@color/colorPrimaryDark"
            >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BOOK TICKETS"
                android:padding="10sp"
                android:id="@+id/bt_book"
                android:textSize="20sp"/>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

我的Listview布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8sp"
    android:gravity="top">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewseat"/>

</LinearLayout>

我的代码是:

public class Seatvisible extends AppCompatActivity {

ArrayList<HashMap<String, String>> busdetails1;

private String BusID, servicedate;
private static String url="http://appsilonz.com/demo/v2/bus_seat_abhi.php?journey_date=2017-01-05&bus_id=20";
private ListView lv;
private TextView busname, from_to, seatsnumber, ticketprice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seatvisible);

    lv = (ListView) findViewById(R.id.listviewbusdetails);
    inilize();

    new Seatparsing().execute();

}

private void inilize() {

    Intent intent = getIntent();
    BusID = intent.getStringExtra("BusID");
    servicedate = intent.getStringExtra("servicedate");
    busname = (TextView) findViewById(R.id.busname);
    from_to = (TextView) findViewById(R.id.from_to);
    seatsnumber = (TextView) findViewById(R.id.seatsnumber);
    ticketprice = (TextView) findViewById(R.id.ticketprice);


}

public class Seatparsing extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {

    String response;
    String jsonstr, seat_name, ticket_status, seatladies;
    ProgressDialog dialog = new ProgressDialog(Seatvisible.this);
    AndroidHttpClient maclient = AndroidHttpClient.newInstance("");

    @Override
    protected void onPreExecute() {
        dialog.setCancelable(false);
        dialog.setMessage("Please wait..");
        dialog.setIndeterminate(false);
        dialog.show();

    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... params) {


        HttpGet request = new HttpGet(url);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            response = maclient.execute(request, responseHandler);
        } catch (ClientProtocolException exception) {
            exception.printStackTrace();
            return null;
        } catch (IOException exception) {
            exception.printStackTrace();
            return null;
        }
        Log.i("Seats", "" + response);
        Log.i("seaturl", "" + url);

        jsonstr = response;
        busdetails1 = new ArrayList<>();

        if (jsonstr != null) {

            HashMap<String, String> map2 = new HashMap<>();
            try {
                JSONArray array1 = new JSONArray(jsonstr);
                for (int i = 0; i < array1.length(); i++) {
                    JSONObject objct1 = array1.getJSONObject(i);
                    JSONArray array2 = objct1.getJSONArray("row1");
                    for (int j = 0; j < array2.length(); j++) {
                        JSONObject objct2 = array2.getJSONObject(j);

                        seat_name = objct2.getString("seat_name ").toString();
                        ticket_status = objct2.getString("ticket_status");
                        seatladies = objct2.getString("seatIsLadies");

                        map2.put("seat_name", seat_name);
                        map2.put("ticket_status", ticket_status);
                        map2.put("seatIsLadies", seatladies);

                    }
                    busdetails1.add(map2);
                }
            } catch (Exception e) {
            e.printStackTrace();
            }
        }

        return busdetails1;

    }

    protected void onPostExecute(ArrayList<HashMap<String, String>> busdetails1) {


        dialog.dismiss();

        customadapter adapter = new customadapter(getApplicationContext(), R.layout.seatlistview, busdetails1);
        lv.setAdapter(adapter);
    }
}

public class customadapter extends ArrayAdapter {

    private ArrayList<HashMap<String, String>> list;
    private int resource;
    private LayoutInflater inflater;

    public customadapter(Context context, int resource, ArrayList<HashMap<String, String>> busdetails1) {
        super(context, resource, busdetails1);

        list = busdetails1;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);


    }

    @NonNull
    @Override
    public View getView(int i, View convertView, ViewGroup parent) {

        convertView = inflater.inflate(resource, null);

        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewseat);

        String seat_name = list.get(i).get("seat_name");
        String ticket_status = list.get(i).get("ticket_status");
        String seatIsLadies = list.get(i).get("seatIsLadies");

        if (!seat_name.equals(null)) {

            if (ticket_status.equals("booked")) {
                if (seatIsLadies.equals("true")) {
                    imageView.setImageResource(R.drawable.reservedladies);
                    imageView.setClickable(false);
                } else {
                    imageView.setImageResource(R.drawable.bookedseat);
                    imageView.setClickable(false);
                }

            } else {
                imageView.setImageResource(R.drawable.available_seat_img);


            }

        } else {

            imageView.setImageDrawable(null);
            imageView.setClickable(false);


        }


        return convertView;
    }
}

}

3 个答案:

答案 0 :(得分:1)

您应该使用dp表示维度,sp表示textSize。目前,您正在使用sp维度。

参见下面编辑的布局,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_seatvisible"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.notebook.runtowin.Activity.Seatvisible">


    <TextView
        android:id="@+id/from_to"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimaryDark"
        android:text="Select your seat"
        android:padding="5dp"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textColor="@color/colorwhite" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@+id/from_to">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FFEB3B"
        android:id="@+id/headinglayout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/busname"
                android:padding="5dp"
                android:text="RUN TO WIN"
                android:textStyle="bold"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/busname"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/seatnumber"
                android:padding="5dp"
                android:text="Seats :"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:text="25,24"
                android:id="@+id/seatsnumber"/>

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ticketprice"
            android:layout_alignParentRight="true"
            android:text="hsdshd"
            android:textStyle="bold"
            android:padding="5dp"
            android:textSize="30sp"
            android:layout_marginRight="10dp"/>

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/headinglayout"
        android:layout_marginTop="3dp"
        android:paddingTop="10dp"
        android:paddingRight="25dp"
        android:paddingLeft="25dp">

        <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/iconsteer"
            android:id="@+id/imageView2"
            android:layout_marginEnd="50dp"
            android:layout_alignParentEnd="true"/>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:layout_below="@+id/imageView2"
            android:layout_marginTop="20dp"
            android:id="@+id/listviewseat">

        </ListView>


    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentEnd="true"
        android:gravity="center_horizontal"
        android:background="@color/colorPrimaryDark"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BOOK TICKETS"
            android:padding="10dp"
            android:id="@+id/bt_book"
            android:textSize="20sp"/>
    </RelativeLayout>
</LinearLayout>

对于ListView布局,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:gravity="top">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewseat"/>

</LinearLayout>

此外,您不能layout_marginRight到此行id

android:layout_marginRight="@+id/seatnumber"

你可能意味着,

android:layout_marginRight="5dp"

请参阅@ Baseem Samy的答案。

答案 1 :(得分:0)

我在ur布局文件中看到填充大小的sp单位。 sp仅适用于字体大小,其中dp或dip适用于所有尺寸。

答案 2 :(得分:0)

在文本视图中使用id android:id =&#34; @ + id / seatsnumber&#34; 你将layout_marginRight设置为一个id,在android中不可用,你不能用视图设置填充

 <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@+id/seatnumber"
                    android:text="25,24"
                    android:id="@+id/seatsnumber"/>