LayoutInflater NullPointerException

时间:2012-05-18 15:49:36

标签: android nullpointerexception layout-inflater

我正在尝试刷新自定义ListViewAdapter;

修改:

当我点击Listview中的一个项目时,它正在调用GoruntuleActivity的屏幕并创建oncreate方法(我假设它正在创建..)然后我调用了一个GoruntuleActivity方法,并且layputInflater进入了这个Activity。 /强>

ListeleActivity.java

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View item,
                    int position, long id) {

                onClickPosition = position;

                final Intent intent = new Intent(ListeleActivity.this,
                        GoruntuleActivity.class);
                startActivity(intent);

                goruntuleNesnesi.isSetListRefresh(onClickPosition , issetlist);
            }
        });

,它正在调用GoruntuleActivity.java

的方法
  public void isSetListRefresh(int onClickPosition, ArrayList<String> issetlist )
    {
        issetlist.set(onClickPosition, "false");
        setRecentOrSeen(issetlist);
        ListviewRefresh();
    }

    public void ListviewRefresh() 
    {
        LayoutInflater mLInflater = LayoutInflater.from(this);

        ListViewAdapter adapter = new ListViewAdapter(
                getApplicationContext(), getKimdenList(), getKonuList(), getRecentOrSeen() ,
                mLInflater);
        adapter.Remove();

    }

现在又给了我另一个例外......

05-18 16:25:22.073: E/AndroidRuntime(807): FATAL EXCEPTION: main
05-18 16:25:22.073: E/AndroidRuntime(807): java.lang.IllegalStateException: System services not available to Activities before onCreate()
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.app.Activity.getSystemService(Activity.java:3526)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.view.LayoutInflater.from(LayoutInflater.java:171)
05-18 16:25:22.073: E/AndroidRuntime(807):  at com.mobil.eposta.GoruntuleActivity.ListviewRefresh(GoruntuleActivity.java:160)
05-18 16:25:22.073: E/AndroidRuntime(807):  at com.mobil.eposta.GoruntuleActivity.isSetListRefresh(GoruntuleActivity.java:155)
05-18 16:25:22.073: E/AndroidRuntime(807):  at com.mobil.eposta.ListeleActivity$1.onItemClick(ListeleActivity.java:137)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.widget.ListView.performItemClick(ListView.java:3382)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.os.Handler.handleCallback(Handler.java:587)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.os.Looper.loop(Looper.java:123)
05-18 16:25:22.073: E/AndroidRuntime(807):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-18 16:25:22.073: E/AndroidRuntime(807):  at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:25:22.073: E/AndroidRuntime(807):  at java.lang.reflect.Method.invoke(Method.java:521)
05-18 16:25:22.073: E/AndroidRuntime(807):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-18 16:25:22.073: E/AndroidRuntime(807):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-18 16:25:22.073: E/AndroidRuntime(807):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

从错误日志中,看起来getApplicationContext()返回null。你什么时候打电话给goruntuleNesnesi.ListviewRefresh();?它在onCreate(...)中吗?如果是这样,那可能是您的问题,因为getApplicationContext(...)将在onCreate(...)初始化之前返回null。

编辑:我仔细查看了堆栈跟踪,看起来你正在按一下按钮调用ListviewRefresh。您是否尝试过使用this而不是getApplicationContext(...)?

编辑2 :试试这个:

ListViewAdapter adapter = new ListViewAdapter(this, getKimdenList(), getKonuList(), getRecentOrSeen() ,mLInflater); 

虽然,我认为可能遭受同样的问题,因为尚未在屏幕上绘制活动并且尚未初始化,您无法对其进行通胀。尝试一下,我们将从那里开始。