navigation drawer activityを使って、navigation drawerをカスタマイズしています。
それぞれのnavigationがタップされた時に、処理を書きたいのですが、エラーが出てしまいます。
MainからMember のintentに移ろうと、
navigation drawerをタップするとエラーになります。
**Main-Member-Sub **
と遷移したいです。
main(navigation drawerで)
Member(リストビューをタップ)
試しに
MainからSubに移すとエラーなく行けました。
Main-Memberに設定するとエラーが出ます。
何が原因でしょうか?
main
@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; }
Member.java
public class Member extends AppCompatActivity { private ListView listView; ArrayList<MemberProfile> memberProfilesList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); listView = (ListView)findViewById(R.id.listView1); //アレイリストのインスタンスを作成 memberProfilesList = new ArrayList<>(); memberProfilesList.add(new MemberProfile("吉田拓郎","農家",R.drawable.human,"農家の吉田拓郎です。最近の趣味はアイスホッケー。好きな食べ物はドリアとピザです。")); memberProfilesList.add(new MemberProfile("静岡じゅん","広報", R.drawable.human1,"広報担当のじゅんです。プリンが大好物!")); memberProfilesList.add(new MemberProfile("ケイティ・スチュワート","広報", R.drawable.human2,"Hi I'm Katie. My job is PR and SNS manager.")); //配列を保持するアダプターのインスタンスと引数を設定 //ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, profiles); 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); } }); } }
エラー
FATAL EXCEPTION: main Process: com.example.navigationdrawer, PID: 19942 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:54) 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)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/31 09:45
2016/03/31 10:52
2016/03/31 11:24
2016/04/01 05:26