列表视图中带有文本视图的搜索栏
Posted
技术标签:
【中文标题】列表视图中带有文本视图的搜索栏【英文标题】:Seekbar with textview in a listview 【发布时间】:2021-05-29 15:53:37 【问题描述】:所以我尝试使用 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= ActivityRecord50930bb 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;
【问题讨论】:
你能分享崩溃报告吗? 它只是突然关闭.. 但这里是错误日志,我把它放在帖子上编辑 是您的应用程序在更改搜索栏值时崩溃,或者它甚至无法打开。 整个活动都打不开,突然停止并回到上一个活动 我更新了问题,因为它只是自动关闭 【参考方案1】:试试这个:
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();
);
您的代码的问题是您在 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;
【讨论】:
mashaallah 谢谢兄弟的回答,但它不起作用。 我已经在活动中设置了适配器 不起作用,先生.. 我尝试了您的解决方案,但我非常感谢您的回答 我明白了,先生,我认为代码会在字体活动中找到搜索栏,但它会从声明的资源中找到 list_row 布局。在规定的资源布局上设置搜索栏后,它可以工作。但是,我实际上希望它使用字体活动布局中的搜索栏。如果您知道,真的很感激答案。 但我想使用单独的搜索栏单独更改列表视图中的每个文本视图大小。我可以在字体大小活动中从 list_row 布局调用 textview 吗?【参考方案2】:我认为在构造函数中,你应该添加下一行:
this.sharedPreferences = this.mContext.getSharedPreferences("SHARED_PREFERENCES_NAME", MODE_PRIVATE);
this.editor = this.sharedPreferences.edit();
如果您还没有这样做并且您还没有创建一个 SharedPreferences 文件,其名称替换为 SHARED_PREFERENCES_NAME 您的应用将再次崩溃,因为它将是 this.shared 首选项的 null 对象引用,同时获得 null 的编辑器价值。
【讨论】:
很抱歉,您能详细解释一下吗? 我包含活动 java 请看一下 是的,我的意思是,代码崩溃是因为您没有为 SP 属性和 Editor 属性分配任何值,您应该将应用程序中的 SP 文件分配为我上面提到过。否则,每次运行代码都会再次崩溃。 还有一件事,你应该在编辑之前启用对 int 值的编辑,在 onProgressChange 函数中添加下一行:editor.edit(); 对不起,编辑器编辑不是将值应用于 textview 吗?不应该把它放在需要价值的其他活动上吗?【参考方案3】: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;
【讨论】:
对不起先生,但活动仍然突然停止并返回上一个活动.. 你在导航中添加了 break 关键字吗?onnavigationitemselected 丹麦语是对的,你应该在主活动导航中添加一个break关键字。 不知道先生,能不能把run终端显示的错误截图? 哦,对不起,等一下,先生以上是关于列表视图中带有文本视图的搜索栏的主要内容,如果未能解决你的问题,请参考以下文章