安卓布局中的javanpe
我开发了一些安卓程序,但它在安卓4.4.2上抛出NLE,在安卓4.1.2上运行良好
我认为问题在于:setContentView(R.layout.drawerlayout); 但是我的抽屉布局没有错误
部分代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.drawerlayout);
if (安卓.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("setting", 0); // 0 - for private mode
login=pref.getBoolean("loged_in", false);
Drawable d=getResources().getDrawable(R.drawable.action_bar_bg);
getActionBar().setBackgroundDrawable(d);
try {
catnum = 1;
addcat("دسته بندی ها",0);
new CateFetch().execute("a","c", "b");
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = arr;// getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
} catch (Exception E) {
}
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
日志:
07-26 09:14:07.519 2167-2167/com.example.安卓.navigationdrawerexample D/dalvikvm﹕ GC_FOR_ALLOC freed 91K, 6% free 2900K/3068K, paused 33ms, total 36ms
07-26 09:14:07.859 2167-2167/com.example.安卓.navigationdrawerexample D/AndroidRuntime﹕ Shutting down VM
07-26 09:14:07.859 2167-2167/com.example.安卓.navigationdrawerexample W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2aa3ba8)
07-26 09:14:07.919 2167-2167/com.example.安卓.navigationdrawerexample D/dalvikvm﹕ GC_FOR_ALLOC freed 28K, 3% free 3383K/3488K, paused 22ms, total 23ms
07-26 09:14:07.929 2167-2167/com.example.安卓.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.安卓.navigationdrawerexample, PID: 2167
java.lang.NullPointerException
at 安卓.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at 安卓.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at 安卓.widget.AbsListView.obtainView(AbsListView.java:2263)
at 安卓.widget.ListView.makeAndAddView(ListView.java:1790)
at 安卓.widget.ListView.fillDown(ListView.java:691)
at 安卓.widget.ListView.fillFromTop(ListView.java:752)
at 安卓.widget.ListView.layoutChildren(ListView.java:1630)
at 安卓.widget.AbsListView.onLayout(AbsListView.java:2091)
at 安卓.view.View.layout(View.java:14817)
at 安卓.view.ViewGroup.layout(ViewGroup.java:4631)
at 安卓.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
at 安卓.view.View.layout(View.java:14817)
at 安卓.view.ViewGroup.layout(ViewGroup.java:4631)
at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:388)
at 安卓.view.View.layout(View.java:14817)
at 安卓.view.ViewGroup.layout(ViewGroup.java:4631)
at com.安卓.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at 安卓.view.View.layout(View.java:14817)
at 安卓.view.ViewGroup.layout(ViewGroup.java:4631)
at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:388)
at 安卓.view.View.layout(View.java:14817)
at 安卓.view.ViewGroup.layout(ViewGroup.java:4631)
at 安卓.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
at 安卓.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at 安卓.view.Choreographer.doCallbacks(Choreographer.java:574)
at 安卓.view.Choreographer.doFrame(Choreographer.java:544)
at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at 安卓.os.Handler.handleCallback(Handler.java:733)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:136)
at 安卓.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
如何解决
# 1 楼答案
它与
setContentView(R.layout.drawerlayout);
无关arr必须在
mPlanetTitles = arr;// getResources().getStringArray(R.array.planets_array);
之前有元素在onCreate的早期添加一个
arr= new String[]{"some string"};
或者更改数组的填充位置
# 2 楼答案
当
ListView
尝试添加其子视图时,必须确保ArrayAdapter
中的所有元素都是非null
。否则会导致NPE。使用AsyncTask
填充元素在没有一些更改的情况下是行不通的,因为它不能保证在ListView
添加子元素之前填充数组如果要从} ,例如^{} ,来填充
AsyncTask
填充ArrayAdapter
,可以使用List
(我建议ArrayList
)作为构造函数参数创建ArrayAdapter
,替换数组或删除数组作为参数,使ArrayAdapter
创建自己的列表。使用在UI线程上运行的AsyncTask
方法中的^{ArrayAdapter