应用程序崩溃并出现日志错误 Resources$NotFoundException: Resource ID #0x0 on using AlertDialog inside ListviewAdapter
App Crashe with log error Resources$NotFoundException: Resource ID #0x0 on using AlertDialog inside ListviewAdapter
我正在创建一个包含一个 Mainactivity 和两个 class 的应用。它包含 Listview 适配器以显示带有图像和详细信息的名称列表。当点击任何图片时...警报对话框会弹出并从选项中,我希望用户能够打电话或发送电子邮件。
之前我使用 Mainactivity class 的代码,它几乎可以工作,但使用 ListviewAdapter 它会崩溃。
这是我的Class
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;
public ListViewAdapter(Context context,
List<WorldPopulation> worldpopulationlist) {
mContext = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<WorldPopulation>();
this.arraylist.addAll(worldpopulationlist);
}
public class ViewHolder {
TextView rank;
TextView country;
TextView population;
ImageView flag;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public WorldPopulation getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.rank = (TextView) view.findViewById(R.id.rank);
holder.country = (TextView) view.findViewById(R.id.country);
holder.population = (TextView) view.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
holder.flag = (ImageView) view.findViewById(R.id.flag);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.rank.setText(worldpopulationlist.get(position).getRank());
holder.country.setText(worldpopulationlist.get(position).getCountry());
holder.population.setText(worldpopulationlist.get(position)
.getPopulation());
// Set the results into ImageView
holder.flag.setImageResource(worldpopulationlist.get(position)
.getFlag());
final String email = worldpopulationlist.get(position).getRank().toString();
final String phone = worldpopulationlist.get(position).getCountry().toString();
// Listen for ListView Item Click
final View finalView = view;
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CharSequence options[] = new CharSequence[]{"Email", "Call"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if(i == 0){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
mContext.startActivity(Intent.createChooser(intent, ""));
}
if(i == 1){
Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
// callIntent.setData(Uri.parse("tel:"+uri));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(callIntent);
}
}
});
builder.show();
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
worldpopulationlist.clear();
if (charText.length() == 0) {
worldpopulationlist.addAll(arraylist);
} else {
for (WorldPopulation wp : arraylist) {
if (wp.getCountry().toLowerCase(Locale.getDefault())
.contains(charText)) {
worldpopulationlist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
错误日志是
Process: com.nepalpolice.test, PID: 30475
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1123)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
at android.content.res.Resources.getLayout(Resources.java:939)
at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:988)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:964)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:981)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:1005)
at com.nepalpolice.test.ListViewAdapter.onClick(ListViewAdapter.java:125)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5018)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
请帮忙。
提前致谢。
Android 具有其正常的 classes,如 AlertDialog,然后它具有其中一些 classes 的支持版本,其中一个是 AlertDialog。这是为了提供与旧 Android 版本的向后兼容性,这些版本可能本身没有这些 classes。然而,它有时会让人感到困惑。
您链接的教程使用的是 Activity 和普通的 AlertDialog。但是,您正在使用 Activity 和 support AlertDialog。这行不通。 AlertDialog 依赖于主题或样式来知道它应该使自己看起来像什么。由于您使用的是普通 Activity,支持 AlertDialog 无法推断它应该是什么样子,因为 Activity 不使用支持包主题。
您可以采取一些措施来解决此问题。
解决此问题的最简单方法是从使用支持的 AlertDialog 更改为使用本机的 AlertDialog。从您的适配器 class 中删除 import android.support.v7.app.AlertDialog;
,然后重新导入 AlertDialog,确保您不再导入支持版本。
第二种最简单的方法是遵循 并为您的 AlertDialog 添加特定的 AppCompat 主题。但是,这不会解决您将来可能 运行 通过混合 AppCompat 和普通 classes 遇到的任何问题。
解决此问题最复杂(但也是最兼容)的方法是转换为使用 AppCompatActivity。您必须更改 Activity 以扩展 AppCompatActivity,并且还更改您的主题(在 styles.xml 中)以拥有 Theme.AppCompat.Light
的父项(删除 .Light
如果你想要一个黑暗的主题)。
我正在创建一个包含一个 Mainactivity 和两个 class 的应用。它包含 Listview 适配器以显示带有图像和详细信息的名称列表。当点击任何图片时...警报对话框会弹出并从选项中,我希望用户能够打电话或发送电子邮件。
之前我使用 Mainactivity class 的代码,它几乎可以工作,但使用 ListviewAdapter 它会崩溃。
这是我的Class
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;
public ListViewAdapter(Context context,
List<WorldPopulation> worldpopulationlist) {
mContext = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<WorldPopulation>();
this.arraylist.addAll(worldpopulationlist);
}
public class ViewHolder {
TextView rank;
TextView country;
TextView population;
ImageView flag;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public WorldPopulation getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.rank = (TextView) view.findViewById(R.id.rank);
holder.country = (TextView) view.findViewById(R.id.country);
holder.population = (TextView) view.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
holder.flag = (ImageView) view.findViewById(R.id.flag);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.rank.setText(worldpopulationlist.get(position).getRank());
holder.country.setText(worldpopulationlist.get(position).getCountry());
holder.population.setText(worldpopulationlist.get(position)
.getPopulation());
// Set the results into ImageView
holder.flag.setImageResource(worldpopulationlist.get(position)
.getFlag());
final String email = worldpopulationlist.get(position).getRank().toString();
final String phone = worldpopulationlist.get(position).getCountry().toString();
// Listen for ListView Item Click
final View finalView = view;
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CharSequence options[] = new CharSequence[]{"Email", "Call"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if(i == 0){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
mContext.startActivity(Intent.createChooser(intent, ""));
}
if(i == 1){
Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
// callIntent.setData(Uri.parse("tel:"+uri));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(callIntent);
}
}
});
builder.show();
}
});
return view;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
worldpopulationlist.clear();
if (charText.length() == 0) {
worldpopulationlist.addAll(arraylist);
} else {
for (WorldPopulation wp : arraylist) {
if (wp.getCountry().toLowerCase(Locale.getDefault())
.contains(charText)) {
worldpopulationlist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
错误日志是
Process: com.nepalpolice.test, PID: 30475
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1123)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
at android.content.res.Resources.getLayout(Resources.java:939)
at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:988)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:964)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:981)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:1005)
at com.nepalpolice.test.ListViewAdapter.onClick(ListViewAdapter.java:125)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5018)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Android 具有其正常的 classes,如 AlertDialog,然后它具有其中一些 classes 的支持版本,其中一个是 AlertDialog。这是为了提供与旧 Android 版本的向后兼容性,这些版本可能本身没有这些 classes。然而,它有时会让人感到困惑。
您链接的教程使用的是 Activity 和普通的 AlertDialog。但是,您正在使用 Activity 和 support AlertDialog。这行不通。 AlertDialog 依赖于主题或样式来知道它应该使自己看起来像什么。由于您使用的是普通 Activity,支持 AlertDialog 无法推断它应该是什么样子,因为 Activity 不使用支持包主题。
您可以采取一些措施来解决此问题。
解决此问题的最简单方法是从使用支持的 AlertDialog 更改为使用本机的 AlertDialog。从您的适配器 class 中删除 import android.support.v7.app.AlertDialog;
,然后重新导入 AlertDialog,确保您不再导入支持版本。
第二种最简单的方法是遵循
解决此问题最复杂(但也是最兼容)的方法是转换为使用 AppCompatActivity。您必须更改 Activity 以扩展 AppCompatActivity,并且还更改您的主题(在 styles.xml 中)以拥有 Theme.AppCompat.Light
的父项(删除 .Light
如果你想要一个黑暗的主题)。