我是新手,刚开始学习,请原谅如果错误很愚蠢。即使查看了其他类似的答案,我仍然无法弄清楚自己做错了什么。任何帮助都将不胜感激。
这是错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.guessthenumberpuzzle, PID: 2428
android.view.InflateException: Binary XML file line #8: addView(View, LayoutParams) is not supported in AdapterView
Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
at android.widget.AdapterView.addView(AdapterView.java:495)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.android.guessthenumberpuzzle.ListAdapter.getView(ListAdapter.java:27)
at android.widget.AbsListView.obtainView(AbsListView.java:2362)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)
at android.widget.ListView.onMeasure(ListView.java:1233)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19734)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
at android.view.View.measure(View.java:19734)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Application terminated.
这里是文件:
Game.java
package com.example.android.guessthenumberpuzzle;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class Game extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
ArrayList<List> arrayList = new ArrayList<>();
public void submit(View view) {
EditText number1 = (EditText)findViewById(R.id.num_1);
int no1=Integer.parseInt(number1.getText().toString());
EditText number2 = (EditText)findViewById(R.id.num_2);
int no2=Integer.parseInt(number2.getText().toString());
EditText number3 = (EditText)findViewById(R.id.num_3);
int no3=Integer.parseInt(number3.getText().toString());
arrayList.add(new List(no1, no2, no3));
ListAdapter arrayAdapter = new ListAdapter(this, arrayList);
ListView listView = (ListView)findViewById(R.id.past_answers);
listView.setAdapter(arrayAdapter);
}
}
activity_game.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkYellow"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:background="@color/lightPurple"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make your guess:"
android:textColor="@android:color/white"
android:textSize="20sp" />
<EditText
android:id="@+id/num_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="1"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
<EditText
android:id="@+id/num_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="1"
android:textColorHint="@android:color/white" />
<EditText
android:id="@+id/num_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:hint="1"
android:textColorHint="@android:color/white" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:onClick="submit"
android:text="Submit" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/past_answers"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/num_1_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/num_2_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/num_3_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ListView>
List.java
package com.example.android.guessthenumberpuzzle;
/**
* Created by Abhijeet on 08-07-2017.
*/
public class List {
private int mNum1;
private int mNum2;
private int mNum3;
public List(int num1, int num2, int num3){
mNum1 = num1;
mNum2 = num2;
mNum3 = num3;
}
public int getNum1(){
return mNum1;
}
public int getNum2(){
return mNum2;
}
public int getNum3(){
return mNum3;
}
}
ListAdapter.java
package com.example.android.guessthenumberpuzzle;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<List> {
public ListAdapter(Context context, ArrayList<List> arrayList) {
super(context, 0, arrayList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
List currentItem = getItem(position);
TextView num1View = (TextView)listItemView.findViewById(R.id.num_1_view);
num1View.setText(currentItem.getNum1());
TextView num2View = (TextView)listItemView.findViewById(R.id.num_2_view);
num2View.setText(currentItem.getNum2());
TextView num3View = (TextView)listItemView.findViewById(R.id.num_3_view);
num3View.setText(currentItem.getNum3());
return listItemView;
}
}