我正在 Android Studio 中开发我的应用程序。 今天,我需要升级到 API33。 申请编译:OK
在我的手机上测试:
值得注意的是,在开发过程中,我忽略了编译错误: “此片段应提供默认构造函数(不带参数的公共构造函数)”和“避免片段中的非默认构造函数:使用默认构造函数加上 Fragment#setArguments(Bundle)”,方法是添加:
lintOptions { 禁用“有效片段” }
到 Gradle。在所有 Android 版本上,除 Api 30 及更高版本外,该应用程序均运行良好。
令我困惑的是,使用 Api 28 和 29 编译的应用程序在 Android 11、12 和 13 所有版本上都能顺利运行。但不幸的是,我必须使用 API33 for Google 进行编译。
你能提出一个解决方案吗?或者有其他语句来替换 LintOptions 吗?
这是我的一个片段的代码:
package com.XXXXXXXX.XXXXX;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
public class HelloFragment extends Fragment {
ListView lst;
String[] maladie = {"Michel","Jack"};
String[] desc = {"Acheteur","Vendeur"};
Integer[] imgid = {R.drawable.michel,R.drawable.jack};
Activity mainActivity;
public HelloFragment(Activity main){
this.mainActivity = main;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.hello_fragment, container, false);
lst = view.findViewById(R.id.listviewhello);
CustomeListviewAdapter customeListview = new CustomeListviewAdapter(this.mainActivity,maladie,desc,imgid);
lst.setAdapter(customeListview);
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if(position == 0) {
Intent intent = new Intent(getContext(),Michel.class);
startActivity(intent);
}if(position == 1) {
Intent intent = new Intent(getContext(),Jack.class);
startActivity(intent);
}
}
});
return view;
}
}
我在家庭课堂上打电话:
helloFragment = inflate.findViewById(R.id.home_hello);
helloFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HelloFragment nextFrag= new HelloFragment(getActivity());
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, nextFrag,"findThisFragment").addToBackStack(null).commit();
}
});
这是我的 Gradle 上的:
apply plugin: 'com.android.application'
android {
signingConfigs {
Signer {
keyAlias 'ssa_key'
keyPassword 'XXXXX'
storeFile file('C:/Users/David/AndroidStudioProjects/ssa_keys.jks')
storePassword 'YYYYYYY'
}
}
lintOptions {
disable 'ValidFragment'
}
compileSdkVersion 33
defaultConfig {
applicationId 'com.XXXXXXXXX.XXXXXXX'
minSdkVersion 17
targetSdkVersion 33
versionCode 14
versionName "3.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.Signer
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.Signer
}
}
productFlavors {
}
buildToolsVersion '34.0.0'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
//noinspection GradleCompatible
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
}
我已经尝试在多个 SDK 版本 26 到 33 上进行编译,但效果相同。
position
在您的数据范围内(假设您的DataList 是您在ListView 中显示的列表)。如果位置出界,您可以根据需要进行处理,也可以干脆忽略它。这将有助于防止单击无效位置时发生崩溃。
您可以使用
getApplication()
代替getContext()
应用程序更安全。
看看这个
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Check if the position is within the bounds of your data
if (position >= 0 && position < yourDataList.size()) {
// Create an intent based on the clicked item
Intent intent;
if (position == 0) {
intent = new Intent(/* you can use getApplication()*/getContext(), Michel.class);
} else if (position == 1) {
intent = new Intent(/* you can use getApplication()*/getContext(), Jack.class);
} else {
// Handle other positions or show a message that the option is not available
// You can also log an error here
return;
}
startActivity(intent);
}
}
});