开始时,ListActivity NullPointerException异常 [英] NullPointerException when starting ListActivity

查看:315
本文介绍了开始时,ListActivity NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作了几个小时上的错误我得到的,当我想开始一个ListActivity。
短期解决方法什么我想做的事:
我有一个正常的菜单主应用程序,从中我要开始设置列表,用户可以添加特定的设置。

我想这样开始在ListView:

  startActivity(新意图(这一点,FileManagerSettings.class));

然后在上创建方法,我得到了错误:

  userSettings = deserializeObject();    如果(userSettings.isEmpty())
    {
        userSettings.add(新SettingItem(照相机,android.os.Environment.DIRECTORY_DCIM,FALSE));
    }
    super.onCreate(冰柱);
    上下文mContext = this.getApplicationContext();
    settingsList =(ListView控件)findViewById(R.id.list_settings);
    settingsList.setAdapter(新CustomSettingsAdapter(mContext,userSettings));

最后一行导致错误。为了更好地理解这里的适配器类的构造函数:

 公共CustomSettingsAdapter(上下文的背景下,ArrayList的< SettingItem> sitems)
 {
     settingsArrayList = sitems;
     mInflater = LayoutInflater.from(上下文);
 }

这是从logcat的错误(对不起,不知道如何格式化右)


  

十一月四日至1日:07:26.756:ERROR / AndroidRuntime(375):致命异常:主要
  11月4日至1日:07:26.756:ERROR / AndroidRuntime(375):了java.lang.RuntimeException:无法启动活动ComponentInfo {org.openintents.filemanager / org.openintents.filemanager.FileManagerSettings}:显示java.lang.NullPointerException
  11月4日至1日:07:26.756:ERROR / AndroidRuntime(375):致:显示java.lang.NullPointerException
  11月4日至1日:07:26.756:ERROR / AndroidRuntime(375):在org.openintents.filemanager.FileManagerSettings.onCreate(FileManagerSettings.java:43)


在我的思维错误造成的,因为上下文的LayoutInflater为空...但我不知道如何让LayoutInflater然后甚至不知道这是不是真的出错...

编辑:错误日志中使用的setContentView():


  

12月4日至1日:06:56.315:ERROR / AndroidRuntime(669):致命异常:主要
  12月4日至1日:06:56.315:ERROR / AndroidRuntime(669):了java.lang.RuntimeException:无法启动活动ComponentInfo {org.openintents.filemanager / org.openintents.filemanager.FileManagerSettings}了java.lang.RuntimeException:您的内容必须有一个ListView的id属性是'android.R.id.list


这是XML的文件:

 <的LinearLayout的android:layout_width =FILL_PARENT
    机器人:ID =@ + ID /列表
    机器人:layout_height =FILL_PARENT
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    < TextView的机器人:ID =@ + ID / settings_list_title
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:文字=@字符串/ settings_header
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:比重=中心/>
    < ListView的机器人:ID =@ + ID /列表
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
    < / LinearLayout中>


解决方案

findViewById()如果Activty的布局已定只返回一个视图的有效参考。看着你在你的问题贴在code,它不会出现你正在调用

 的setContentView(R.layout.whatever_your_layout_file_id);

您变量settingsList,那将是一个空引用

I'm working for hours on an error I get, when I want to start a ListActivity. Short workaround about what I want to do: I have a main application with a normal menu, from which I want to start a setting list where the user can add specific settings.

I'm trying to start the ListView like this:

startActivity(new Intent(this, FileManagerSettings.class));

Then in the on Create method I got the error:

        userSettings = deserializeObject();

    if(userSettings.isEmpty())
    {
        userSettings.add(new SettingItem("Camera", android.os.Environment.DIRECTORY_DCIM, false));
    }
    super.onCreate(icicle);
    Context mContext = this.getApplicationContext(); 
    settingsList = (ListView)findViewById(R.id.list_settings);
    settingsList.setAdapter(new CustomSettingsAdapter(mContext, userSettings));

The last line causes the error. For better understanding here the Constructor of the adapter class:

     public CustomSettingsAdapter(Context context, ArrayList<SettingItem> sitems)
 {
     settingsArrayList = sitems;
     mInflater = LayoutInflater.from(context);
 }

And here is the error from the logcat: (Sorry, don't know how to format it right)

04-01 11:07:26.756: ERROR/AndroidRuntime(375): FATAL EXCEPTION: main 04-01 11:07:26.756: ERROR/AndroidRuntime(375): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.NullPointerException 04-01 11:07:26.756: ERROR/AndroidRuntime(375): Caused by: java.lang.NullPointerException 04-01 11:07:26.756: ERROR/AndroidRuntime(375): at org.openintents.filemanager.FileManagerSettings.onCreate(FileManagerSettings.java:43)

In my thinking the error is caused because the LayoutInflater of the context is null... But I don't know how to get the LayoutInflater then and even don't know if this is really the error...

edit: error log with using setContentView():

04-01 12:06:56.315: ERROR/AndroidRuntime(669): FATAL EXCEPTION: main 04-01 12:06:56.315: ERROR/AndroidRuntime(669): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.openintents.filemanager/org.openintents.filemanager.FileManagerSettings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

this is the xml-File:

<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/list"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/settings_list_title"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/settings_header"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center" />
    <ListView android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>

解决方案

findViewById() only returns a valid reference to a View if the layout of the Activty has been set. Looking at the code you have pasted in to your question, it does not appear that you are making a call to

setContentView(R.layout.whatever_your_layout_file_id);

Your variable settingsList, would then be a null reference

这篇关于开始时,ListActivity NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆