首页 > 解决方案 > 列表视图中带有文本视图的搜索栏

问题描述

所以我尝试使用 seekbar 在列表视图的自定义适配器中获取进度值。代码上的一切都很好,但是当我尝试启动它时,自动关闭并转到以前的活动。谁能知道我的代码有什么问题?

我将来会将共享首选项用于应用程序中的其他活动,这就是我使用它的原因。

这里是自定义适配器代码

public class PerrowAdapter extends ArrayAdapter<Perrow> {

    int textSize = 30;

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    private Context mContext;
    private int mResource;
    private ArrayList<Perrow> objects;

    public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
        this.objects = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(mResource, parent, false);

        TextView tvArabic = convertView.findViewById(R.id.arabictext);
        TextView tvTransliteration = convertView.findViewById(R.id.transliteration);
        TextView tvTranslation = convertView.findViewById(R.id.translation);

        //seekbar

        int progress = sharedPreferences.getInt("progress", 0);
        
        SeekBar seekbar1 = convertView.findViewById(R.id.seekbar1);
        seekbar1.setProgress(progress);

        //settextsize
        tvArabic.setTextSize(textSize+seekbar1.getProgress()); // size 30sp

        Perrow perrow = objects.get(position);

        tvArabic.setText(perrow.getArabic());
        tvTransliteration.setText(perrow.getTransliteration());
        tvTranslation.setText(perrow.getTranslation());

        //seekbar

        seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            int progressNew = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textSize = textSize + (progress - progressNew);
                progressNew = progress;
                tvArabic.setTextSize(textSize);
                editor.putInt("progress", progress);
                editor.apply();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        return convertView;
    }
}

编辑

这里是错误日志

2021-02-27 23:05:26.130 3959-3959/? E/example.munaja: Unknown bits set in runtime_flags: 0x8000
2021-02-27 23:05:26.148 3959-3959/? E/libc: Access denied finding property "runtime.mmitest.isrunning"
2021-02-27 23:05:26.345 3959-3995/com.example.munajat E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@b449e48

这是活动java

public class fontsize extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{


    //SharedPreferences

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    ListView listView;


    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fontsize);

        //sharedpreferences

        sharedPreferences = getSharedPreferences("fontsize", MODE_PRIVATE);
        editor = sharedPreferences.edit();


        //seekbar

        String[] arabic = getResources().getStringArray(R.array.fontsize);
        String[] transliteration = getResources().getStringArray(R.array.transliteration);
        String[] translation = getResources().getStringArray(R.array.translation);


        listView = findViewById(R.id.listView);

        //Create Data

        ArrayList<Perrow> list = new ArrayList<>();
        for(int i = 0; i < arabic.length; i++){
            list.add(new Perrow(arabic[i], transliteration[i], translation[i]));
        }



        ///customadapter

        PerrowAdapter perrowAdapter = new PerrowAdapter(this, R.layout.list_row, list);
        listView.setAdapter(perrowAdapter);


        //definingmenudrawer

        drawerLayout = findViewById(R.id.drawer_layout4);
        navigationView = findViewById(R.id.nav_view4);
        toolbar = findViewById(R.id.toolbar4);


        //toolbar

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //toolbar.setNavigationIcon(R.drawable.ic_toolbar);

        toolbar.setTitle("");
        toolbar.setSubtitle("");
        //toolbar.setLogo(R.drawable.ic_toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_dehaze_24);

        //navdrawermenu

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_home);

    }

    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()) {


            case R.id.nav_home:
                Intent intent = new Intent(fontsize.this, home.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            case R.id.nav_fontsize:
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

}

从运行终端我得到了这个

V/AudioManager: querySoundEffectsEnabled...
I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@6c623a
V/ActivityThread: callActivityOnCreate
D/ActivityThread: add activity client record, r= ActivityRecord{50930bb token=android.os.BinderProxy@6c623a {com.example.munajat/com.example.munajat.fontsize}} token= android.os.BinderProxy@6c623a
D/HiTouch_PressGestureDetector: onAttached, package=com.example.munajat, windowType=1, mHiTouchRestricted=false
I/BlockMonitor: dispatchingThrewException In MainThread
D/AndroidRuntime: Shutting down VM
I/QarthLog: [PatchStore] createDisableExceptionQarthFile
    [PatchStore] create disable file for com.example.munajat uid is 10425
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.munajat, PID: 9485
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setProgress(int)' on a null object reference
        at com.example.munajat.PerrowAdapter.getView(PerrowAdapter.java:57)
        at android.widget.AbsListView.obtainView(AbsListView.java:2494)
        at android.widget.HwAbsListView.obtainView(HwAbsListView.java:1139)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1446)
        at android.widget.ListView.onMeasure(ListView.java:1352)
        at android.view.View.measure(View.java:24742)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:24742)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:24742)
        at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:792)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:583)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:355)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.solveVerticalMatchConstraint(Direct.java:636)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:407)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.verticalSolvingPass(Direct.java:446)
        at androidx.constraintlayout.solver.widgets.analyzer.Direct.solvingPass(Direct.java:178)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.layout(ConstraintWidgetContainer.java:642)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solveLinearSystem(BasicMeasure.java:159)
        at androidx.constraintlayout.solver.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:290)
        at androidx.constraintlayout.solver.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:119)
        at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1578)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1690)
        at android.view.View.measure(View.java:24742)
        at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1562)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:728)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1562)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:728)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:896)
        at android.view.View.measure(View.java:24742)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3211)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1974)
E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2295)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1857)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8089)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1057)
        at android.view.Choreographer.doCallbacks(Choreographer.java:875)
        at android.view.Choreographer.doFrame(Choreographer.java:776)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1042)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
I/Process: Sending signal. PID: 9485 SIG: 9


以及我用来启动字体大小活动的主要活动

public class maintext extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    int textSize = 30;

    TextView textView;

    //sharedpreferencesvariables

    SharedPreferences prefs;
    SharedPreferences.Editor editor;

    //variablesmenudrawer

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintext);

        // definingtextview

        textView = findViewById(R.id.maintext);

        //sharedpreferencescall

        prefs = getSharedPreferences("fontsize", MODE_PRIVATE);

        int progress = prefs.getInt("progress",0);

        //settextsizeaccordingseekbar

        textView.setTextSize(textSize+progress);


        //definingmenudrawer

        drawerLayout = findViewById(R.id.drawer_layout1);
        navigationView = findViewById(R.id.nav_view1);
        toolbar = findViewById(R.id.toolbar1);


        //toolbar

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        //toolbar.setNavigationIcon(R.drawable.ic_toolbar);
        toolbar.setTitle("");
        toolbar.setSubtitle("");
        //toolbar.setLogo(R.drawable.ic_toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_dehaze_24);

        //navdrawermenu

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_home);

    }


    @Override
    public void onBackPressed() {

        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()) {


            case R.id.nav_home:
                Intent intent = new Intent(maintext.this, home.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            case R.id.nav_fontsize:
                Intent intent1 = new Intent(maintext.this, fontsize.class);
                startActivity(intent1);
                finish();

        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

标签: javaandroidandroid-studiosharedpreferencescustom-adapter

解决方案


尝试这个:

       PerrowAdapter perrowAdapter = new PerrowAdapter(getApplicationContext(), R.layout.list_row, list);
    listView.setAdapter(perrowAdapter);

添加中断关键字:

    case R.id.nav_home:
            Intent intent = new Intent(fontsize.this, home.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
            break;
        case R.id.nav_fontsize:
           break;
    }

这样做应该有效:

    SeekBar seekbar1 = (SeekBar)convertView.findViewById(R.id.seekbar1);//typecast the seekbar

首先在你的 onprogresschanged 上试试这个:

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(progress!=null){
            textSize = textSize + (progress - progressNew);
            progressNew = progress;
            tvArabic.setTextSize(textSize);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        editor.putInt("progress", progress);
            editor.apply();

        }
    });
   

您的代码的问题是您在 fontsize 活动中添加了 NavigationItemselected,而不是在 mainActivity 中添加它。完成此操作后,您需要在 NavigationItemselected 中向您的 fontsize 活动和主页活动发送一个意图。

编辑

   private Context mContext;
private int mResource;
private ArrayList<Perrow> objects;
 TextView tvArabic;
    TextView tvTransliteration;
    TextView tvTranslation;
public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> objects, TextView tvArabic,
    TextView tvTransliteration,
    TextView tvTranslation) {
    super(context, resource, objects);
    this.mContext = context;
    this.mResource = resource;
    this.objects = objects;
    this.tvArabic=tvArabic;
    this.tvTransliteration=tvTransliteration;
   this.tvTranslation=tvTranslation;
}
  

推荐阅读