我正在尝试自定义列表视图,以便在按Enter键时,新项目可以自动添加到列表视图中。这是我第一次做到这一点并得到了bug。我并不擅长阅读LogCat,但我预测问题来自于无法对我在自定义列表视图布局中创建的TextView进行夸大。请帮我找出问题!!!谢谢
这是我的主要活动
package com.example.auto_multi_listview;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
public class Auto_Multi extends Activity {
private AutoCompleteTextView auto;
private MultiAutoCompleteTextView multi;
private Button button;
private ListView list_view;
private ArrayList<Content> list;
private MyOwnAdapter listview_adapter;
private String[] hint = {"Aiden", "Aaron", "Adam", "Audrey", "Adrian", "Adriana",
"Bob","Brooklyn","Barbara", "Nathan", "Natalie", "Noah",
"Christine", "Caroline", "Cassie", "Henry", "Halley", "Ryan", "Stephanie",
"Grabriel", "Max", "Paul", "Poppy", "Mathew", "Maria", "Kitty", "Lily",
"Hannah", "Alice", "Amie", "Tim", "Olivia", "Jenifer", "Jake", "Jack",
"Emily", "Everlyn", "Susan", "Steuart", "David", "Michael", "Madison",
"Diana", "Dylan", "Daren", "Sheena", "Yvone"};
private int[] image = {R.drawable.bulbasaur, R.drawable.caterpie, R.drawable.charmander,
R.drawable.nidoranf, R.drawable.nidoranm, R.drawable.pikachu, R.drawable.sandshrew,
R.drawable.squirtle, R.drawable.teddiursa, R.drawable.vulpix};
protected void onCreate(Bundle x) {
super.onCreate(x);
setContentView(R.layout.activity_auto_multi);
initializeAuto();
initializeMulti();
initializeListView();
initializeButton();
}
private void initializeAuto() {
auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Auto_Multi.this,
android.R.layout.simple_list_item_1, hint);
auto.setAdapter(adapter);
}
private void initializeMulti() {
multi = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Auto_Multi.this,
android.R.layout.simple_list_item_1, hint);
multi.setAdapter(adapter);
multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private void initializeListView() {
list_view = (ListView) findViewById(R.id.listView1);
list = new ArrayList<Content>();
listview_adapter = new MyOwnAdapter(Auto_Multi.this, R.layout.custom_listview, list);
list_view.setAdapter(listview_adapter);
list_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final int initial_position = position;
AlertDialog.Builder builder = new AlertDialog.Builder(Auto_Multi.this);
builder.setTitle("Hold on!!!!");
builder.setMessage("Are you sure that you want to delete this item???");
builder.setPositiveButton("Definitely", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.remove(initial_position);
listview_adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Oops, nooo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
return false;
}
});
}
private void initializeButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Content content = new Content();
Random random = new Random();
int index = random.nextInt(image.length);
content.setImage(image[index]);
content.setLine1(auto.getText().toString());
content.setLine2(multi.getText().toString());
list.add(content);
listview_adapter.notifyDataSetChanged();
}
});
}
}
这是我的MainActivity布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.auto_multi_listview.Auto_Multi" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:completionThreshold="1"
android:hint="@string/hint1" >
<requestFocus />
</AutoCompleteTextView>
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/autoCompleteTextView1"
android:layout_alignStart="@+id/autoCompleteTextView1"
android:layout_alignEnd="@+id/autoCompleteTextView1"
android:layout_alignRight="@+id/autoCompleteTextView1"
android:layout_below="@+id/autoCompleteTextView1"
android:layout_marginTop="10dp"
android:ems="10"
android:completionThreshold="1"
android:hint="@string/hint2" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/multiAutoCompleteTextView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/enter" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1" >
</ListView>
内容是我创建的类,我认为它不会导致错误
这是我的CustomAdapter
package com.example.auto_multi_listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyOwnAdapter extends ArrayAdapter<Content> {
private Activity context;
private int layoutId;
private ArrayList<Content> list;
public MyOwnAdapter(Activity context, int layoutId, ArrayList<Content> list) {
super(context, layoutId, list);
this.context = context;
this.layoutId = layoutId;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_listview, parent, false);
ImageView image = (ImageView) customView.findViewById(R.id.imageView1);
TextView line1 = (TextView) customView.findViewById(R.id.textView1);
TextView line2 = (TextView) customView.findViewById(R.id.textView2);
Content content = list.get(position);
image.setImageResource(content.getImage());
line1.setText(content.getLine1()+"");
line2.setText(content.getLine2()+"");
return customView;
}
}
其布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.auto_multi_listview.Auto_Multi" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/abc_ab_share_pack_mtrl_alpha"
android:contentDescription="@string/content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:layout_toEndOf="@+id/imageView1"
android:gravity="center"
android:textStyle="@string/bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="0dp"
android:layout_toRightOf="@+id/imageView1"
android:layout_toEndOf="@+id/imageView1"
android:textStyle="@string/bold"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</RelativeLayout>
并记录CAt
12-03 07:34:30.228: D/dalvikvm(26650): GC_EXTERNAL_ALLOC freed 466K, 49% free 3027K/5895K, external 2747K/2773K, paused 33ms
12-03 07:34:39.957: W/KeyCharacterMap(26650): Can't open keycharmap file
12-03 07:34:39.957: W/KeyCharacterMap(26650): Error loading keycharmap file '/data/usr/keychars/qtouch-obp-ts.kcm.bin'. hw.keyboards.131072.devname='qtouch- obp-ts'
12-03 07:34:39.957: W/KeyCharacterMap(26650): Can't open keycharmap file
12-03 07:34:39.957: W/KeyCharacterMap(26650): Error loading keycharmap file '/system/usr/keychars/qtouch-obp-ts.kcm.bin'. hw.keyboards.131072.devname='qtouch-obp-ts'
12-03 07:34:39.957: W/KeyCharacterMap(26650): Can't open keycharmap file
12-03 07:34:39.957: W/KeyCharacterMap(26650): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-03 07:34:40.088: W/IInputConnectionWrapper(26650): showStatusIcon on inactive InputConnection
12-03 07:55:25.677: W/Resources(29275): Converting to int: TypedValue{t=0x3/d=0x13c "bold" a=4 r=0x7f0a0015}
12-03 07:55:25.677: D/AndroidRuntime(29275): Shutting down VM
12-03 07:55:25.677: W/dalvikvm(29275): threadid=1: thread exiting with uncaught exception (group=0x40018560)
12-03 07:55:25.687: E/AndroidRuntime(29275): FATAL EXCEPTION: main
12-03 07:55:25.687: E/AndroidRuntime(29275): android.view.InflateException: Binary XML file line #18: Error inflating class android.widget.TextView
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
12-03 07:55:25.687: E/AndroidRuntime(29275): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-03 07:55:25.687: E/AndroidRuntime(29275): at com.example.auto_multi_listview.MyOwnAdapter.getView(MyOwnAdapter.java:32)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.AbsListView.obtainView(AbsListView.java:1456)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.ListView.measureHeightOfChildren(ListView.java:1288)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.ListView.onMeasure(ListView.java:1199)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.View.measure(View.java:8322)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.View.measure(View.java:8322)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.View.measure(View.java:8322)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.View.measure(View.java:8322)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.ViewRoot.performTraversals(ViewRoot.java:842)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.ViewRoot.handleMessage(ViewRoot.java:1862)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.os.Looper.loop(Looper.java:130)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.app.ActivityThread.main(ActivityThread.java:3806)
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.reflect.Method.invoke(Method.java:507)
12-03 07:55:25.687: E/AndroidRuntime(29275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-03 07:55:25.687: E/AndroidRuntime(29275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-03 07:55:25.687: E/AndroidRuntime(29275): at dalvik.system.NativeStart.main(Native Method)
12-03 07:55:25.687: E/AndroidRuntime(29275): Caused by: java.lang.reflect.InvocationTargetException
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.reflect.Constructor.constructNative(Native Method)
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
12-03 07:55:25.687: E/AndroidRuntime(29275): ... 29 more
12-03 07:55:25.687: E/AndroidRuntime(29275): Caused by: java.lang.NumberFormatException: unable to parse 'bold' as integer
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.Integer.parse(Integer.java:383)
12-03 07:55:25.687: E/AndroidRuntime(29275): at java.lang.Integer.parseInt(Integer.java:372)
12-03 07:55:25.687: E/AndroidRuntime(29275): at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.content.res.TypedArray.getInt(TypedArray.java:254)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.TextView.<init>(TextView.java:798)
12-03 07:55:25.687: E/AndroidRuntime(29275): at android.widget.TextView.<init>(TextView.java:446)
12-03 07:55:25.687: E/AndroidRuntime(29275): ... 32 more
答案 0 :(得分:2)
替换:
android:textStyle="@string/bold"
使用:
android:textStyle="bold"