Android Fragments Need Help:Android 11及更高版本的碎片导航错误

mwyxok5s  于 2023-11-19  发布在  Android
关注(0)|答案(1)|浏览(109)

我正在Android Studio中开发我的应用程序。今天,我需要升级到API 33。应用程序编译:OK
在我的手机上测试:

  • 开场:好的
  • 内部导航:OK
  • 片段导航:应用程序崩溃

值得注意的是,在开发过程中,我忽略了编译错误:“此片段应提供默认构造函数(没有参数的公共构造函数)”和“避免片段中的非默认构造函数:使用默认构造函数加上片段#setArguments(Bundle)”,方法是添加:
lintOptions { disable 'ValidFragment' }
到Gradle.在所有Android版本上,该应用程序运行良好,除了API 30及以上版本。
令我困惑的是,用Api 28和29编译的应用程序在Android 11、12和13的所有版本上都能顺利运行。但不幸的是,我必须用Google的API 33编译。
你能建议一个解决方案吗?或者有其他的声明来取代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上编译,但都是一样的。

hof1towb

hof1towb1#

position在您的数据范围内(假设您在ListView中显示的列表是yourDataList)。如果位置超出范围,您可以根据需要处理它或直接忽略它。这将有助于防止单击无效位置时崩溃。
您可以使用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);
                }
            }
        });

字符串

相关问题