gpt4 book ai didi

java - Android AlertDialog 因 NullPointerException 而崩溃

转载 作者:行者123 更新时间:2023-11-30 02:50:57 24 4
gpt4 key购买 nike

我尝试将 HTML 放入 AlertDialog 中的 Listviewitem 中。因此我找到了一些 fragment 并最终将它们与我的 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)));

自定义文本行.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();

最佳答案

您的 costomdialog.xml 没有 ID 为 singlerowTextView。因此 tv 为空,当您调用 setText 时会导致 NullPointerException

从您的评论看来,singlerow 是一个带有 textview 的布局

所以改变这个

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)

完整示例

public class MainActivity extends Activity {

String names[]={"A","B","C"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Onlineliste");

final LayoutInflater inflater = getLayoutInflater();
final View convertView2 = (View) inflater.inflate(R.layout.costomdialog, null);
builder.setView(convertView2);
ListView lv = (ListView)convertView2.findViewById(R.id.customListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.costomdialog, names)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;

if (null == convertView) {
row = inflater.inflate(R.layout.customtextrow, null);
} else {
row = convertView;
}

TextView tv = (TextView)row.findViewById(R.id.singlerow);
tv.setText(names[position]);
//tv.setText(getItem(position));

return row;
}

};
lv.setAdapter(adapter);
AlertDialog alert = builder.create();
alert.show();
}
}

xml是一样的,没有变化

快照:

enter image description here

编辑 2:

why does the Html.fromHtml not work? :/ See my names ArrayList final ArrayList names = new ArrayList();
String[] splitted = response.split("\|"); for(String name:splitted) { names.add(Html.fromHtml("" + name.split("\+")1 + "").toString()); }

拆分字符串并将其添加到数组列表中。在将文本设置为 textview 的同时设置 html。

关于java - Android AlertDialog 因 NullPointerException 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com