当我在列表项中调用警报对话框时,我的应用程序崩溃了。我的应用程序列出了汽车,每个项目都有2个按钮,每个按钮调用alertdialog为是/否答案。该应用程序崩溃“你需要使用Theme.AppCompat主题(或后代)与此活动。”每次我按下按钮。应用代码如下:
AndroidManifest:
<activity android:name=".SplashScreen" android:theme="@style/generalnotitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CarActivity" android:theme="@style/generalnotitle"></activity>
</application>
风格:
<resources>
<style name="generalnotitle" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:screenOrientation">portrait</item>
</style>
</resources>
CarActivity.java
public class CarActivity extends AppCompatActivity {
String[] car_tag, car_makemodel, car_owner_id;
TypedArray car_pic;
String[] aa_owner_id, aa_owner_name, aa_owner_tlf;
String tlf, name;
List<CarItem> carItems;
ListView mylistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("list start", "list start" );
getSupportActionBar().hide();
setContentView(R.layout.activity_list);
mylistview = (ListView) findViewById(R.id.LList);
setUpListView();
}
public void setUpListView() {
carItems = new ArrayList<CarItem>();
// gets car info
car_pic = getResources().obtainTypedArray(R.array.a_carpic);
car_tag = getResources().getStringArray(R.array.a_mat);
car_makemodel = getResources().getStringArray(R.array.a_makemodel);
car_owner_id = getResources().getStringArray(R.array.a_owner);
//defines items
for (int i = 0; i < car_tag.length; i++){
CarItem item = new CarItem(car_pic.getResourceId(i, -1), car_tag[i], car_makemodel[i], car_owner_id[i]);
Log.e("CarActivity", car_tag[i] );
carItems.add(item);
}
//gets available owners
aa_owner_id = getResources().getStringArray(R.array.a_id);
aa_owner_name = getResources().getStringArray(R.array.a_name);
aa_owner_tlf = getResources().getStringArray(R.array.a_tlf);
//populates the list
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
mylistview.setAdapter(adapter);
}
}
CarAdapter.java
public class CarAdapter extends BaseAdapter {
Context context;
List<CarItem> carItems;
String [] a_owner_id, a_owner_names, a_owner_tlf;
String name, tlf, owner_id;
CarAdapter(Context context, List<CarItem> carItems, String [] a_owner_id, String [] a_owner_names, String [] a_owner_tlf){
this.context = context;
this.carItems = carItems;
this.a_owner_id = a_owner_id;
this.a_owner_names = a_owner_names;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.row_car,null);
holder = new ViewHolder();
holder.car_pic = (ImageView) convertView.findViewById(R.id.car_pic);
holder.tag = (TextView) convertView.findViewById(R.id.tag);
holder.makemodel = (TextView) convertView.findViewById(R.id.make_model);
holder.owner = (TextView) convertView.findViewById(R.id.owner);
holder.blck = (Button) convertView.findViewById(R.id.block);
holder.mov = (Button) convertView.findViewById(R.id.move);
CarItem row_pos = carItems.get(position);
holder.car_pic.setImageResource(row_pos.getCar_pic());
holder.tag.setText(row_pos.getTag());
holder.makemodel.setText(row_pos.getMakemodel());
owner_id = row_pos.getOwner();
getOwner(owner_id);
holder.owner.setText(name);
holder.blck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBlockClick();
}
});
holder.mov.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("CarAdapter move click", owner_id );
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private void onBlockClick() {
Log.e("CarActivity", "CLICK block button" );
AlertDialog.Builder alertDlg = new AlertDialog.Builder(context);
alertDlg.setMessage("Do you wish to inform " + name + "?");
alertDlg.setCancelable(false);
alertDlg.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get owner phone number
getNumber(owner_id);
Log.e("CarActivity YES DIALOG", tlf );
//Toast.makeText(getApplicationContext(),"Afasta-me o teu cangalho...", Toast.LENGTH_SHORT).show();
}
});
alertDlg.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("CarActivity", "CLICK NO DIALOG" );
}
});
alertDlg.create().show();
}
}
解决方案:当我设置适配器时,我发送“getApplicationContext()”,而是发送“this”,这样我就可以将上下文发送到适配器。
CarActivity.java:
之前:
CarAdapter adapter = new CarAdapter(getApplicationContext(), carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
后:
CarAdapter adapter = new CarAdapter(this, carItems, aa_owner_id , aa_owner_name, aa_owner_tlf);
答案 0 :(得分:0)
您是否尝试过使用对话框主题的构造函数?
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.generalnotitle);
然后为构建器添加其余的相关代码。
注意,主题应指向对话框主题(Theme.AppCompat.Light.Dialog.Alert)。查看这篇文章了解更多详情。
答案 1 :(得分:0)
有两个AlertDialog
类:常规android.app.AlertDialog
和支持/ appcompat版本android.support.v7.app.AlertDialog
。我无法分辨您要导入哪一个,但后者要求您使用给定Context
的appcompat主题,如果您已经在使用AppCompat库并且您的活动已扩展,那么这应该不是问题来自AppCompatActivity
。 (如果您没有使用AppCompat,那么您应确保使用常规AlertDialog
。)