gpt4 book ai didi

java - Android 主题.AppCompat 主题

转载 作者:行者123 更新时间:2023-12-03 00:57:22 26 4
gpt4 key购买 nike

当我在列表项中调用警报对话框时,我的应用程序崩溃了。我的应用程序列出了汽车,每个项目都有 2 个按钮,每个按钮都会调用警报对话框来回答是/否。应用程序崩溃并显示“您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代)”。每次我按下按钮。应用代码如下:

Android list :

<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);

最佳答案

您是否尝试过使用也采用对话框主题的构造函数?

试试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.generalnotitle);

然后放置构建器的其余相关代码。

请注意,主题应指向对话框主题 (Theme.AppCompat.Light.Dialog.Alert)。查看这篇文章了解更多详细信息。

http://www.materialdoc.com/alerts/

关于java - Android 主题.AppCompat 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092129/

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