我正在创建一种介绍公司成员的企业应用程序。
我想在点击导航抽屉列表时更改意图。
当点击导航抽屉(R.id.member)时,我在setAdapter()上得到NullPointer Exception,但我不知道如何修复它。
请帮忙。
主要
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_corporate) {
// Handle the camera action
} else if (id == R.id.nav_member) {
Intent member_intent = new Intent();
member_intent.setClassName("com.example.navigationdrawer", "com.example.navigationdrawer.Member");
startActivity(member_intent);
} else if (id == R.id.nav_history) {
} else if (id == R.id.nav_business) {
} else if (id == R.id.nav_contact) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
错误日志
Process: com.example.navigationdrawer, PID: 740
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer/com.example.navigationdrawer.Member}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.navigationdrawer.Member.onCreate(Member.java:51)
at android.app.Activity.performCreate(Activity.java:6018)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
listview布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
android:orientation="horizontal"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1" />
</RelativeLayout>
Member.java(点击导航栏列表时更改的意图)
public class Member extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
final ListView listView = (ListView)findViewById(R.id.listView1);
ArrayList<MemberProfile> memberProfilesList;
memberProfilesList = new ArrayList<>();
memberProfilesList.add(new MemberProfile("Kate","CEO",R.drawable.human,"I'm CEO。"));
memberProfilesList.add(new MemberProfile("Ben","PR Manager", R.drawable.human1,"Love sweets"));
memberProfilesList.add(new MemberProfile("Alex","SNS Manager", R.drawable.human2,"My job is PR and SNS manager."));
CustomAdapter customAdapter = new CustomAdapter(this, memberProfilesList);
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MemberProfile memberProfile = (MemberProfile)listView.getItemAtPosition(position);
Intent sub = new Intent();
sub.putExtra("list_names", memberProfile.getName());
sub.putExtra("list_jobs",memberProfile.getJob());
sub.putExtra("list_images",memberProfile.getImage());
sub.putExtra("list_comments",memberProfile.getComment());
sub.setClassName("com.example.navigationdrawer", "com.example.navigationdrawer.SubActivity");
startActivity(sub);
}
});
}
}
打破listView.setAdapter调试错误
Process: com.example.navigationdrawer, PID: 27652
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at com.example.navigationdrawer.CustomAdapter.getView(CustomAdapter.java:24)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15636)
at android.view.ViewGroup.layout(ViewGroup.java:4967)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5786)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
SubActivity
public class SubActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_activity);
TextView text = (TextView) findViewById(R.id.textView);
TextView text2 = (TextView) findViewById(R.id.textView2);
TextView text3 = (TextView) findViewById(R.id.textView3);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Intent sub = getIntent();
String name = sub.getStringExtra("list_names");
String job = sub.getStringExtra("list_jobs");
int image = sub.getIntExtra("list_images", R.drawable.human);
String comment = sub.getStringExtra("list_comments");
text.setText(name);
text2.setText(job);
imageView.setImageResource(image);
text3.setText(comment);
}
}
text.setText(名称);
text2.setText(作业);
imageView.setImageResource(图像);
text3.setText(评语);
这些显示了android studio上的NPE警告(黄色突出显示)