在列表视图中带textview的搜索栏



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

我将来会在应用程序的其他活动中使用sharedpreferences,所以这就是我使用它的原因。

这里是自定义适配器代码

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

试试这个:

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

添加break关键字:

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

你的代码的问题是,你已经添加了NavigationItemselected在你的字体活动,而不是添加它在你的mainActivity。完成这些之后,你需要发送一个intent到你的NavigationItemselected中的fontsize activity和home activity。

编辑

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

我认为在构造函数中,您应该添加以下行:

this.sharedPreferences = this.mContext.getSharedPreferences("SHARED_PREFERENCES_NAME", MODE_PRIVATE);
this.editor = this.sharedPreferences.edit();

如果你没有做过&你还没有创建一个SharedPreferences文件,它的名字取代了SHARED_PREFERENCES_NAME,你的应用程序将再次崩溃,因为它将是this的空对象引用。在获得空值的编辑器时共享首选项。

public PerrowAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Perrow> 
objects) {
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
this.objects = objects;
this.sharedPreferences = this.mContext.getSharedPreferences("progress", 
MODE_PRIVATE);
}
@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);
this.editor = this.sharedPreferences.edit();
progressNew = progress;
tvArabic.setTextSize(textSize);
this.editor.putInt("progress", progress);
this.editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return convertView;
}

最新更新