该应用程序可以在具有api 22的android上完美运行,但是当我使用更高版本时会崩溃。
我创建了一个新的导航抽屉,并且效果很好,并且在添加代码之前仍可以在更高的api上运行。该代码旨在从数据库获取数据并将其作为项目动态添加到菜单中。
当我将android与api 19-22一起使用时,代码运行良好,但在更高版本上崩溃。我怎样才能解决这个问题 ?
这是我添加的代码
private void addMenuItemInNavMenuDrawer() {
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navView.getMenu();
Menu submenu = menu.addSubMenu("");
Result = tutClass.loadAllData();
if (Result.equals("Success")){
tutID = tutClass.getId();
title = tutClass.getTitle();
for (int a = 0; a< title.length;a++){
final int b = a + 1;
submenu.add(a + 1, Integer.valueOf(tutID[a]) + 1 , Menu.NONE, title[a])
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
setItemOnClickItem(b);
return false;
}
});
}
}
else {
showMessage(Result);
}
navView.invalidate();
}
// ============================================= ================================================== ===============================
private void setItemOnClickItem(int id){
try{
llLayout.removeAllViews();
Result = tutClass.searchData(id);
if (Result.equals("Success")){
code = tutClass.getCode();
title = tutClass.getTitle();
topic = tutClass.getTopic();
TextView title1 = new TextView(TutwithNavigation.this);
title1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
title1.setText(String.valueOf(title[0]));
title1.setAllCaps(true);
title1.setTextSize(50);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
title1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
title1.setBackgroundColor(000);
llLayout.addView(title1);
if (code.length != 0){
code = code[0].split("\\`");
}
if (topic.length != 0){
topic = topic[0].split("\\`");
}
if ((code.length == 0) && (topic.length != 0)){
for (int a = 0; a < topic.length;a++){
TextView topic1 = new TextView(TutwithNavigation.this);
topic1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
topic1.setText(String.valueOf(topic[a]));
topic1.setBackgroundColor(0001);
topic1.setTextSize(30);
llLayout.addView(topic1);
}
}
else if ((topic.length == 0) && (code.length != 0)){
for (int a = 0; a < code.length;a++){
TextView code1 = new TextView(TutwithNavigation.this);
code1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
code1.setText(String.valueOf(code[a]));
code1.setTextSize(30);
code1.setBackgroundColor(1);
llLayout.addView(code1);
}
}
else{
for (int a = 0; a < code.length;a++){
TextView topic1 = new TextView(TutwithNavigation.this);
topic1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
topic1.setText(String.valueOf(topic[a]));
topic1.setBackgroundColor(0001);
topic1.setTextSize(30);
llLayout.addView(topic1);
TextView code1 = new TextView(TutwithNavigation.this);
code1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
code1.setText(String.valueOf(code[a]));
code1.setTextSize(30);
code1.setBackgroundColor(1);
llLayout.addView(code1);
}
}
}
else {
showMessage(Result);
}
}catch (Exception e){ showMessage(e.toString());}
}
此代码的目标是当我单击导航抽屉中菜单上的项目时,在约束布局上添加TextView。
这是oncreate方法
public void Start(){
llLayout = (LinearLayout) findViewById(R.id.tutLayout);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutwith_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
Start();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
addMenuItemInNavMenuDrawer(); // THE APPLICATION STARTS NOT RUNNING HERE. EVERYTHING STARTS WHEN I ADD THIS
}