我无法在 listView
行中点击按钮内部来扩展我的自定义对话框布局。当我从listView
行点击启动对话框时,它工作正常,但是从行内的按钮点击我得到 Android.View.InflateException
。
以下是适配器的 getView
方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
final LayoutInflater inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.planets_row, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.zodIcon = (ImageView) convertView.findViewById(R.id.zodiacIcon);
viewHolder.planetName = (TextView) convertView.findViewById(R.id.planetName);
viewHolder.planetDegree = (TextView)convertView.findViewById(R.id.planetDegree);
viewHolder.zodiacName = (TextView)convertView.findViewById(R.id.zodiacName);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.planetExplanation);
viewHolder.openDetail = (Button)convertView.findViewById(R.id.planetDetailsBtn);
convertView.setTag(viewHolder);
viewHolder.openDetail.setTag(position);
viewHolder.openDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, viewHolder.planetName.getText().toString(), Toast.LENGTH_SHORT).show();
final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_details, null);
dialog.setView(view);
TextView explan = (TextView)view.findViewById(R.id.explanationDetail);
Button ok = (Button)view.findViewById(R.id.ok);
dialog.setTitle(viewHolder.planetName.getText() +
" " + viewHolder.planetDegree.getText() + " " + viewHolder.zodiacName.getText());
dialog.setIcon(viewHolder.zodIcon.getBackground());
explan.setText(descrip);
dialog.create();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((DialogInterface) dialog).dismiss();
return;
}
});
dialog.show();
}
});
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
planetTitle = getPlanetName(itemsList.get(position).get("planet_id"));
planetDeg = itemsList.get(position).get("degrees")+"\u00B0";
zodiacName = getZodiacName(itemsList.get(position).get("zodiac_id"));
descrip = itemsList.get(position).get("content").toString();
viewHolder.planetName.setText(planetTitle);
viewHolder.planetDegree.setText(" " + planetDeg);
viewHolder.zodiacName.setText(zodiacName);
viewHolder.tvDescription.setText(descrip);
viewHolder.zodIcon.setBackgroundResource(getZodiacDrawable(zodiacName));
return convertView;
}
我尝试了其他组合,例如:
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.dialog_details, null);
dialog.setView(view);
修改:我已在 print logcat
下面加入
提前致谢:)
10-20 11:50:47.431: E/AndroidRuntime(27409): FATAL EXCEPTION: main
10-20 11:50:47.431: E/AndroidRuntime(27409): Process: com.astrolome, PID: 27409
10-20 11:50:47.431: E/AndroidRuntime(27409): android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.astrolome.PlanetAdapter$1.onClick(PlanetAdapter.java:71)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.View.performClick(View.java:4633)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.View$PerformClick.run(View.java:19330)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Handler.handleCallback(Handler.java:733)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Handler.dispatchMessage(Handler.java:95)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.os.Looper.loop(Looper.java:157)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.app.ActivityThread.main(ActivityThread.java:5356)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Method.invoke(Method.java:515)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-20 11:50:47.431: E/AndroidRuntime(27409): at dalvik.system.NativeStart.main(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): Caused by: java.lang.reflect.InvocationTargetException
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Constructor.constructNative(Native Method)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
10-20 11:50:47.431: E/AndroidRuntime(27409): ... 19 more
10-20 11:50:47.431: E/AndroidRuntime(27409): Caused by: java.lang.NumberFormatException: Invalid int: "@2131361805"
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.invalidInt(Integer.java:137)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.parse(Integer.java:374)
10-20 11:50:47.431: E/AndroidRuntime(27409): at java.lang.Integer.parseInt(Integer.java:365)
10-20 11:50:47.431: E/AndroidRuntime(27409): at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:122)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.content.res.TypedArray.getInt(TypedArray.java:255)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.widget.TextView.<init>(TextView.java:1488)
10-20 11:50:47.431: E/AndroidRuntime(27409): at android.widget.TextView.<init>(TextView.java:893)
10-20 11:50:47.431: E/AndroidRuntime(27409): ... 22 more
答案 0 :(得分:1)
尝试在适配器构造函数中传递您的活动,并将其存储在类成员中,就像您的上下文一样。然后,替换此行
final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
与
final AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
答案 1 :(得分:0)
尝试更改此内容:
ok.setOnClickListener(new View.OnClickListener() {...}
到此:
ok.setOnClickListener(new DialogInterface.OnClickListener() {...}
我不知道这是否是您代码中唯一的问题,但这似乎是一个问题。