带导航抽屉的空指针

Posted

技术标签:

【中文标题】带导航抽屉的空指针【英文标题】:Nullpointer with navigation drawer 【发布时间】:2015-02-23 19:15:43 【问题描述】:

我正在尝试使用导航抽屉将数据放入幻灯片菜单应用程序中的列表视图。我创建了一个列表视图并定义了适配器,但是当我运行它时,我在 logcat 中得到了空指针

这是我的代码:

package chnt.appchef.com;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity 
   String[] menu;
   DrawerLayout dLayout;
   ListView dList;
   ArrayAdapter<String> adapter;

   ArrayAdapter<String> adapter1;

   String[] countryArray = "India", "Pakistan", "USA", "UK";

   @Override
   protected void onCreate(Bundle savedInstanceState) 
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

ArrayAdapter adapter1 = new ArrayAdapter<String>(this, 
          R.layout.activity_listview, countryArray);

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);


    menu = new String[]"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us";
      dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      dList = (ListView) findViewById(R.id.left_drawer);
      adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
      dList.setAdapter(adapter);
  dList.setSelector(android.R.color.holo_blue_dark);
      dList.setOnItemClickListener(new OnItemClickListener()
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long id) 
      dLayout.closeDrawers();
      Bundle args = new Bundle();
      args.putString("Menu", menu[position]);
      Fragment detail = new DetailFragment();
      detail.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
      fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
    
      );
  

这是细节片段类

package chnt.appchef.com;
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;
  public class DetailFragment extends Fragment 
TextView text;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) 
    View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
    String menu = getArguments().getString("Menu");
    text= (TextView) view.findViewById(R.id.detail);
    text.setText(menu);
    return view;

  

我也定义了列表视图

logcat

12-26 16:12:57.796: E/AndroidRuntime(19145): FATAL EXCEPTION: main
12-26 16:12:57.796: E/AndroidRuntime(19145): java.lang.RuntimeException: Unable to start activity ComponentInfochnt.appchef.com/chnt.appchef.com.MainActivity: java.lang.NullPointerException
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.os.Looper.loop(Looper.java:137)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread.main(ActivityThread.java:5493)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at java.lang.reflect.Method.invoke(Method.java:525)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at dalvik.system.NativeStart.main(Native Method)
12-26 16:12:57.796: E/AndroidRuntime(19145): Caused by: java.lang.NullPointerException
12-26 16:12:57.796: E/AndroidRuntime(19145):    at chnt.appchef.com.MainActivity.onCreate(MainActivity.java:31)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.Activity.performCreate(Activity.java:5372)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
12-26 16:12:57.796: E/AndroidRuntime(19145):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-26 16:12:57.796: E/AndroidRuntime(19145):    ... 11 more
12-26 16:13:00.066: I/Process(19145): Sending signal. PID: 19145 SIG: 9

【问题讨论】:

MainActivity 中的第 31 行是什么? What is a Null Pointer Exception, and how do I fix it? 的可能重复项 【参考方案1】:

您正在创建和初始化实例适配器 1,但将实例适配器分配给尚未初始化且为空的列表视图。

所以改变

listView.setAdapter(adapter);

listView.setAdapter(adapter1);

【讨论】:

【参考方案2】:

在这里您正在创建适配器 1 并分配适配器。看这里:

ArrayAdapter adapter1 = new ArrayAdapter<String>(this, 
          R.layout.activity_listview, countryArray);

ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

改变这个:

listView.setAdapter(adapter1);

【讨论】:

【参考方案3】:

将 listview.setAdapter(adapter) 改为 listview.setAdapter(adapter1)

【讨论】:

以上是关于带导航抽屉的空指针的主要内容,如果未能解决你的问题,请参考以下文章

操作栏上不确定的进度条上的空指针异常

Android中的导航抽屉空指针异常

具有多个活动的导航抽屉。 - 空指针异常

导航抽屉中的谷歌地图 - 空指针异常,哪个是正确的片段?

向上导航的空指针android

java中的空指针异常怎么解决