在Android Studio中使TextView出现错误

时间:2017-11-25 02:42:14

标签: android

当我尝试加载自定义列表时出现以下错误。以前它工作得很好。但现在它崩溃了。 我认为这是因为我修改了build.gradle文件,但我无法找到解决方案。

enter image description here

这是我的CustomList.java的xml代码list_single.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textviewmain"
                android:layout_width="330dp"
                android:layout_height="103dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="0dp"
                android:layout_marginTop="6dp"
                android:background="@drawable/box_attribute_2"
                android:lineSpacingExtra="8sp"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageButton
                android:id="@+id/callbutton"
                android:layout_width="36dp"
                android:layout_height="33dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:background="@android:color/holo_red_dark"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.971"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="0.14999998"
                app:srcCompat="@android:drawable/ic_menu_call" />

            <ImageButton
                android:id="@+id/messagebutton"
                android:layout_width="37dp"
                android:layout_height="33dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:background="@android:color/holo_red_dark"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.974"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="0.8"
                app:srcCompat="@android:drawable/sym_action_email" />

            <TextView
                android:id="@+id/textview_name"
                android:layout_width="226dp"
                android:layout_height="37dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:paddingLeft="3dp"
                android:text="TextView"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="21sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.693"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/textview_address"
                android:layout_width="181dp"
                android:layout_height="39dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:paddingBottom="3dp"
                android:paddingLeft="12dp"
                android:text="TextView"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.658"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="1.0"
                tools:layout_editor_absoluteX="98dp" />

            <TextView
                android:id="@+id/textview_blood"
                android:layout_width="28dp"
                android:layout_height="39dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:paddingBottom="3dp"
                android:paddingLeft="3dp"
                android:text="T"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.213"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="1.0" />

            <ImageView
                android:id="@+id/imageview1"
                android:layout_width="60dp"
                android:layout_height="86dp"

                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/textview_number"
                android:layout_width="198dp"
                android:layout_height="31dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:paddingLeft="3dp"
                android:paddingTop="7dp"
                android:text="TextView"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintHorizontal_bias="0.525"
                app:layout_constraintLeft_toLeftOf="@+id/textviewmain"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintTop_toTopOf="@+id/textviewmain"
                app:layout_constraintVertical_bias="0.446" />

            <TextView
                android:id="@+id/textview_blood2"
                android:layout_width="80dp"
                android:layout_height="31dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="110dp"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:paddingBottom="3dp"
                android:paddingLeft="3dp"
                android:text="15km"
                android:textAlignment="viewStart"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/textviewmain"
                app:layout_constraintEnd_toEndOf="@+id/textview_number"
                app:layout_constraintRight_toRightOf="@+id/textviewmain"
                app:layout_constraintStart_toStartOf="@+id/textview_number"
                app:layout_constraintTop_toTopOf="@+id/textviewmain" />


        </android.support.constraint.ConstraintLayout>

    </TableRow>
</TableLayout>

Customlist.java

package org.raktadaan.jiwandaan.fragments.search;

/**
 * Created by ayush on 8/13/17.
 */

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import org.raktadaan.jiwandaan.R;
import org.raktadaan.jiwandaan.fragments.post_message.message_activity;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;

public class CustomList extends ArrayAdapter<String> {

    private final Context context;
    private final String[] name,location,blood_group,contact,gender,distance,photo;
    private final Double[] distan=new Double[10000];

    public CustomList(Context context,
    String[] name, String[] location, String[] blood_group, String[] contact, String[] gender, String[] photo, String[] distance){
        super(context, R.layout.list_single, name);
        this.context = context;
        this.name = name;

        this.location = location;
        this.blood_group = blood_group;
        this.contact = contact;
        this.gender = gender;
        this.photo=photo;
        this.distance=distance;

       // this.imageId = imageId;

    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(context);
        final View rowView= inflater.inflate(R.layout.list_single, null, true);
        ImageButton call, message;


        TextView mains, names, locations, bloods,number,dist;
        ImageView propic;

        call=(ImageButton) rowView.findViewById(R.id.callbutton);
        call.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+contact[position]));
                context.startActivity(callIntent);


            }
        });

        message = (ImageButton) rowView.findViewById(R.id.messagebutton);



        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(context,message_activity.class);
                i.putExtra("e1_name",name[position]);
                i.putExtra("e2_number",contact[position]);
                i.putExtra("e3_location",location[position]);
                i.putExtra("e4_blood",blood_group[position]);
                context.startActivity(i);
            }
        });

        mains = (TextView) rowView.findViewById(R.id.textviewmain);
        propic= (ImageView) rowView.findViewById(R.id.imageview1);
        /*names= (TextView) rowView.findViewById(R.id.textview_name);
        locations= (TextView) rowView.findViewById(R.id.textview_address);
        bloods= (TextView) rowView.findViewById(R.id.textview_blood);

        number= (TextView) rowView.findViewById(R.id.textview_number);
        dist= (TextView) rowView.findViewById(R.id.textview_blood2);
//        Log.e("name",name[position]);
        names.setText(name[position]);
        locations.setText(location[position]);
        bloods.setText(blood_group[position]);
        number.setText(contact[position]);
        distan[position]= Double.parseDouble(distance[position]);
        distance[position]= String.format("%.1f", new BigDecimal(distan[position]));
        dist.setText(distance[position]+"km");*/


        byte[] images= Base64.decode(photo[position],0);
        Bitmap picture= BitmapFactory.decodeByteArray(images,0,images.length);

        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        picture.compress(Bitmap.CompressFormat.JPEG,1/100,byteArrayOutputStream);

        Drawable dp=new BitmapDrawable(context.getResources(),picture);


       /* if(gender[position]!=null && gender[position].equals("Others"))
            propic.setImageResource(imageId[2]);
        else if(gender[position]!=null && gender[position].equals("Female"))
            propic.setImageResource(imageId[1]);
        else*/
            propic.setImageDrawable(dp);



        return rowView;
    }
    public static boolean hasPermissions(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
}

请帮助我。谢谢

3 个答案:

答案 0 :(得分:0)

您应该在getView()方法

中使用以下代码
final View rowView= inflater.inflate(R.layout.list_single, null, false);

您应该使用false作为第三个参数而不是true

访问here了解inflate方法的详细信息

答案 1 :(得分:0)

你的问题是

  

错误膨胀TextView

所以我们先检查TextView

在您的代码中。您只需使用mains = (TextView) rowView.findViewById(R.id.textviewmain);

因此,您可以在xml代码中查看 android:background="@drawable/box_attribute_2"

然后检查box_attribute_2

您也可以使用android:background="@android:color/holo_red_dark"来尝试。

答案 2 :(得分:-1)

修复设计部分中的TextView,以便您可以纠正错误