我尝试将HTML放入AlertDialog中的Listviewitem中。 因此我找到了一些片段,最后将我的AlertDialog设置在一起。
AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
builder.setTitle("Onlineliste");
final LayoutInflater inflater = getLayoutInflater();
final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
builder.setView(convertView2);
ListView lv = (ListView)convertView2.findViewById(R.id.customListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView) {
row = inflater.inflate(R.layout.costumdialog, null);
} else {
row = convertView;
}
TextView tv = (TextView)row.findViewById(R.id.singlerow);
tv.setText(Html.fromHtml(getItem(position)));
//tv.setText(getItem(position));
return row;
}
};
lv.setAdapter(adapter);
AlertDialog alert = builder.create();
alert.show();
问题是,setAdapter()
迫使应用程序崩溃并出现NullPointerException
xml文件等都存在,我认为它们没有问题。
任何人都可以帮助我吗?
编辑:
06-12 07:26:45.537: E/AndroidRuntime(28036): FATAL EXCEPTION: main
06-12 07:26:45.537: E/AndroidRuntime(28036): java.lang.NullPointerException
06-12 07:26:45.537: E/AndroidRuntime(28036): at com.flipflopdev.epvp_aj1987_chat.ChatActivity$1$1.getView(ChatActivity.java:82)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.AbsListView.obtainView(AbsListView.java:2075)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.ListView.measureHeightOfChildren(ListView.java:1476)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.ListView.onMeasure(ListView.java:1339)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5005)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-12 07:26:45.537: E/AndroidRuntime(28036): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2141)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.View.measure(View.java:12948)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1206)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.os.Looper.loop(Looper.java:156)
06-12 07:26:45.537: E/AndroidRuntime(28036): at android.app.ActivityThread.main(ActivityThread.java:4977)
06-12 07:26:45.537: E/AndroidRuntime(28036): at java.lang.reflect.Method.invokeNative(Native Method)
06-12 07:26:45.537: E/AndroidRuntime(28036): at java.lang.reflect.Method.invoke(Method.java:511)
06-12 07:26:45.537: E/AndroidRuntime(28036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-12 07:26:45.537: E/AndroidRuntime(28036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-12 07:26:45.537: E/AndroidRuntime(28036): at dalvik.system.NativeStart.main(Native Method)
R.layout.costumdialog
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
ChatActivity中的第82行
tv.setText(Html.fromHtml(getItem(position)));
customtextrow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/singlerow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingLeft="5dp">
</TextView>
</RelativeLayout>
新代码:
AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
builder.setTitle("Onlineliste");
final LayoutInflater inflater = getLayoutInflater();
//final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
//builder.setView(convertView2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (null == convertView) {
row = inflater.inflate(R.layout.text, null);
} else {
row = convertView;
}
TextView tv = (TextView)row.findViewById(R.id.singlerow);
tv.setText(Html.fromHtml(getItem(position)));
//tv.setText(getItem(position));
return row;
}
};
AlertDialog alert = builder.create();
ListView lv = (ListView)alert.findViewById(R.id.customListView);
lv.setAdapter(adapter);
alert.show();
答案 0 :(得分:1)
您的costomdialog.xml
没有身份TextView
的{{1}}。因此singlerow
为空,当您调用setText时会导致tv
。
从你的评论看起来,singlerow是一个带有textview的布局
所以改变这个
NullPointerException
到
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.costumdialog, names)
编辑:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.singlerow, names)
完整示例
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.customtextrow.xml, names)
xml是相同的无变化
对齐:
编辑2:
为什么Html.fromHtml不起作用? :/看我的名字ArrayList final ArrayList names = new ArrayList();
串[] splitted = response.split(“\ |”); for(String name:splitted){ names.add(Html.fromHtml(“”+ name.split(“\ +”)1 +“”)。toString()); }
拆分String将其添加到arraylist。在将文本设置为textview时设置html。
答案 1 :(得分:1)
问题是你在没有任何TextView的情况下膨胀R.layout.costumdialog
你不需要在ListView中扩充ListView,只需要将TextView添加到R.layout.costumdialog
创建名为text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/singlerow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
将其更改为此
if (null == convertView) {
row = inflater.inflate(R.layout.text, null);
这不是将适配器设置为ListView
的正确方法,您需要从AlertDialog获取ListView
引用以使其能够使用适配器。
示例:强>
<强>错强>
ListView lv = (ListView)convertView2.findViewById(R.id.customListView);
从右强>
AlertDialog alert = builder.create();
ListView lv = (ListView)alert.findViewById(R.id.customListView);
完整代码:
AlertDialog.Builder builder = new AlertDialog.Builder(ChatActivity.this);
builder.setTitle("Onlineliste");
final LayoutInflater inflater = getLayoutInflater();
final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
builder.setView(convertView2);
AlertDialog alert = builder.create();
ListView lv = (ListView)alert.findViewById(R.id.customListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ChatActivity.this, R.layout.text, names)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
TextView tv = (TextView)row.findViewById(R.id.singlerow);
tv.setText(Html.fromHtml(getItem(position)));
//tv.setText(getItem(position));
return row;
}
};
lv.setAdapter(adapter);
alert.show();
答案 2 :(得分:-1)
我认为问题出在inflater上,
final View convertView2 = (View) inflater.inflate(R.layout.costumdialog, null);
您正在投射不需要的视图。
尝试这样的事情
protected Dialog onCreateDialog() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(TicTacToe.this)
//.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(getTitleText())
.setView(textEntryView)
.setPositiveButton("JOIN GAME", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
EditText et = (EditText)findViewById(R.id.username_edit);
playerName = et.getText().toString();
}
catch (Exception e)
{
}
}
})
.create();
return null;
}