我有一个导航抽屉,当我点击其中的一个项目时,它应该打开另一个活动。所以我做了一个开关,并没有得到任何错误。仍然当我启动应用程序时它仍然崩溃。需要任何帮助我能得到!!! Java在下面
package com.gfo.enexis;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.id;
import static android.R.attr.start;
import static android.R.id.edit;
import static android.R.id.list;
import static com.gfo.enexis.R.id.button;
import static com.gfo.enexis.R.id.lv1;
import static com.gfo.enexis.R.id.nav_acount;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
registerClickCallBack();
OnNavigationItemSelected();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitByBackKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void exitByBackKey() {
AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Weet u zeker dat u wilt uitloggen?")
.setPositiveButton("Log uit", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
finish();
//close();
}
})
.setNegativeButton("Nee", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
EditText NotitieEt;
ListView ListNote;
ArrayList<String> lv = new ArrayList<>();
public void NoteAdd(View view) {
ListNote = (ListView) findViewById(lv1);
NotitieEt = (EditText) findViewById(R.id.notitie);
String note1 = NotitieEt.getText().toString();
if (lv.contains(note1)) {
Toast.makeText(this, "Notitie bestaat al", Toast.LENGTH_LONG).show();
} else if (note1 == null || note1.trim().equals("")) {
Toast.makeText(this, "Imput can't be empty", Toast.LENGTH_LONG).show();
} else {
lv.add(note1);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lv);
ListNote.setAdapter(adapter);
((EditText) findViewById(R.id.notitie)).setText("");
}
}
private void registerClickCallBack() {
ListNote = (ListView) findViewById(lv1);
ListNote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paret, View viewClicked, int position, long id) {
lv.remove(position);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, lv);
ListNote.setAdapter(adapter);
}
});
}
MenuItem item;
public boolean OnNavigationItemSelected() {
int id = item.getItemId();
switch (id) {
case R.id.nav_acount:
Intent i = new Intent(MainActivity.this, DashBoard1.class);
startActivity(i);
break;
case R.id.nav_dashboard:
Intent o = new Intent(MainActivity.this, Dashboard2.class);
startActivity(o);
}
return true;
}
}
这些是我得到的错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.gfo.enexis / com.gfo.enexis.MainActivity}:java.lang.NullPointerException:尝试调用接口方法'int android.view.MenuItem.getItemId ()'在空对象引用
上和
引起:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int android.view.MenuItem.getItemId()'
答案 0 :(得分:0)
我认为您的问题是您尚未在onCreate()中注册导航抽屉。 使用此:
NavigationView navigationView = (NavigationView) findViewById(R.id.yourIdHere);
navigationView.setNavigationItemSelectedListener(this);
此外,要处理导航抽屉点击,请覆盖方法:
onNavigationItemSelected(MenuItem item)
答案 1 :(得分:0)
您没有初始化
MenuItem item;
所以它是NULL!我认为你正在实施&#34; OnNavigationItemSelected&#34;以错误的方式。你为什么要打电话&#34; OnNavigationItemSelected();&#34; in&#34; onCreate()&#34;方法
只需按照官方教程,您就可以了:
https://developer.android.com/training/implementing-navigation/nav-drawer.html