在Youtube上关注本教程:https://www.youtube.com/watch?v=E6vE8fqQPTE&index=8&list=PLgCYzUzKIBE8TUoCyjomGFqzTFcJ05OaC。我的代码很好,说没有错误,但是当我在Android Studio 3.0.1中的模拟器上运行它时,应用程序崩溃了。我使用的是编译/目标SDK 27,最小18.模拟器API 27。
堆栈跟踪:
02-15 13:07:56.424 26483-26483/com.example.sjdav.listadapter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sjdav.listadapter, PID: 26483
android.view.InflateException: Binary XML file line #0: Can't convert value at index 4 to dimension: type=0x12
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 4 to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7794)
at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1976)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1874)
at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1872)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
<!--PROBLEM LINE--> at com.example.sjdav.listadapter.PersonListAdapter.getView(PersonListAdapter.java:63)</--PROBLEM LINE-->
at android.widget.AbsListView.obtainView(AbsListView.java:2365)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2164)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:761)
at android.view.View.layout(View.java:19659)
at android.view.ViewGroup.layout(ViewGroup.java:6075)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2496)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2212)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
XML活动:activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sjdav.listadapter.MainActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
custom_list_view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
tools:context="com.example.sjdav.listadapter.PersonListAdapter">
<TextView
android:id="@+id/textView1"
android:gravity="center"
android:textAlignment="center"
android:text="textView1"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="33.3"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="66.6">
<TextView
android:id="@+id/textView2"
android:gravity="center"
android:text="textView2"
android:layout_width="match_parent"
android:layout_height="30dp" />
<TextView
android:id="@+id/textView3"
android:gravity="center"
android:text="textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="@+id/textView2"/>
</LinearLayout>
</LinearLayout>
PersonListAdapter类:
public class PersonListAdapter extends ArrayAdapter<Person> {
private static final String TAG = "PersonListAdapter";
private Context mContext;
private int mResource;
private int lastPosition = 1;
static class ViewHolder {
TextView name;
TextView birthday;
TextView sex;
// ImageView icon;
// ProgressBar progressBar;
// int position;
}
public PersonListAdapter(@NonNull Context context, int resource, ArrayList<Person> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Get the persons information
String name = getItem(position).getName();
String birthday = getItem(position).getBirthday();
String sex = getItem(position).getSex();
// Create the person object with the information
Person person = new Person(name, birthday, sex);
Log.d(TAG, "getView: PersonListAdapter Person created.");
// Create the anim View
final View result;
// ViewHolder Object
ViewHolder viewHolder;
// Create the LayoutInflater
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Log.d(TAG, "getView: before convertView.");
<!--PROBLEM LINE-->convertView = layoutInflater.inflate(mResource, parent, false);</--PROBLEM LINE-->
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.birthday = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.sex = (TextView) convertView.findViewById(R.id.textView3);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;
viewHolder.name.setText(person.getName());
viewHolder.birthday.setText(person.getBirthday());
viewHolder.sex.setText(person.getSex());
return convertView;
}
}
MainActivity类:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: startedMain");
ListView mListView = (ListView) findViewById(R.id.listView1);
Person m1 = new Person("John", "01-01-2000", "male");
Person m2 = new Person("John", "01-01-2000", "male");
Person m3 = new Person("John", "01-01-2000", "male");
Person m4 = new Person("John", "01-01-2000", "male");
Person m5 = new Person("John", "01-01-2000", "male");
Person m6 = new Person("John", "01-01-2000", "male");
Person m7 = new Person("John", "01-01-2000", "male");
Person m8 = new Person("John", "01-01-2000", "male");
Person m9 = new Person("John", "01-01-2000", "male");
Person m10 = new Person("John", "01-01-2000", "male");
Person m11 = new Person("John", "01-01-2000", "male");
Person m12 = new Person("John", "01-01-2000", "male");
Person m13 = new Person("John", "01-01-2000", "male");
Person m14 = new Person("John", "01-01-2000", "male");
Person m15 = new Person("John", "01-01-2000", "male");
Person m16 = new Person("John", "01-01-2000", "male");
Person m17 = new Person("John", "01-01-2000", "male");
Person m18 = new Person("John", "01-01-2000", "male");
Person m19 = new Person("John", "01-01-2000", "male");
Person m20 = new Person("John", "01-01-2000", "male");
// Create ArrayList of Person Objects
ArrayList<Person> personArrayList = new ArrayList<>();
personArrayList.add(m1);
personArrayList.add(m2);
personArrayList.add(m3);
personArrayList.add(m4);
personArrayList.add(m5);
personArrayList.add(m6);
personArrayList.add(m7);
personArrayList.add(m8);
personArrayList.add(m9);
personArrayList.add(m10);
personArrayList.add(m11);
personArrayList.add(m12);
personArrayList.add(m13);
personArrayList.add(m14);
personArrayList.add(m15);
personArrayList.add(m16);
personArrayList.add(m17);
personArrayList.add(m18);
personArrayList.add(m19);
personArrayList.add(m20);
Log.d(TAG, "onCreate: personArrayList created.");
// Person List Adapter
PersonListAdapter adapter = new PersonListAdapter(this, R.layout.custom_list_view, personArrayList);
mListView.setAdapter(adapter);
}
}
人类:
public class Person {
private String name;
private String birthday;
private String sex;
public Person(String name, String birthday, String sex) {
this.name = name;
this.birthday = birthday;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sjdav.listadapter">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
StackTrace中的异常指向layoutInflater.inflate(mResource,parent,false)。我对此并不熟悉。堆栈顶部:
进程:com.example.sjdav.listadapter,PID:26483 android.view.InflateException:二进制XML文件行#0:无法将索引4处的值转换为维度:type = 0x12 引起:java.lang.UnsupportedOperationException:无法将索引4处的值转换为dimension:type = 0x12。
因此发生了一些转换错误,我无法隔离它。请帮忙!提前谢谢!
答案 0 :(得分:0)
问题出在custom_list_view.xml
。低于TextView
margin属性创建此Exception
<TextView
android:id="@+id/textView3"
android:gravity="center"
android:text="textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="@+id/textView2"/>
检查此行
android:layout_marginTop="@+id/textView2"
@+id/textView2
这不是有效的保证金值。
StackTrace Log也说这个。
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 4 to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7794)
使用有效值。
答案 1 :(得分:0)
为dp
提供layout_marginTop
中的值,如
<TextView
android:id="@+id/textView3"
android:gravity="center"
android:text="textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"/>