安卓:NullPointerException异常,这可能是空在这里? [英] Android: NullPointerException, what could be null here?

查看:203
本文介绍了安卓:NullPointerException异常,这可能是空在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从logcat的:

  6月11日至26日:43:40.643:E / AndroidRuntime(1163):致命异常:AsyncTask的#1
...
6月11日至26日:43:40.643:E / AndroidRuntime(1163):了java.lang.RuntimeException:执行doInBackground发生错误()
...
6月11日至26日:43:40.643:E / AndroidRuntime(1163):因:显示java.lang.NullPointerException
...
6月11日至26日:43:40.643:E / AndroidRuntime(1163):在com.example.mymobiletest.SearchTask.doInBackground(SearchTask.java:134)

行号134 ED =(EditText上)mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch); 。现在这行的运行表明, mainActivity (它是传递给这个构造函数的主要活动的一个实​​例的AsyncTask )不是。那么还有什么可能是在这一行,这是造成 NullPointerException异常

  @覆盖
保护字符串doInBackground(虚空...... voidParameters){
    编的EditText = NULL;
    如果(mainActivity!= NULL){
        ED =(EditText上)mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);
    }其他{
        返回mainActivity是空的罪魁祸首。
    }


编辑: - 我认为,由于我没有的更改的在的UI doInBackground()(但从UI仅读出​​),所以这应该不是一个问题。但我仍然尝试这种在上preExecute(),因为上preExecute 在UI执行线程,但我仍然得到NPE在同一个声明。

  @覆盖
    在preExecute保护无效(){
        编的EditText = NULL;
        如果(mainActivity!= NULL){
            ED =(EditText上)mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch); // ****** NPE
        }其他{
            Log.i(TAGmainActivity是空的罪魁祸首。);
        }
        。SEARCHQUERY = ed.getText()的toString();
    }


解决方案

您应该让您的任务静态内部类,除非你需要在同一时间运行这些任务多。然后,你应该完全分开的类。

doInBackground执行一个任务,然后将结果返回给onPostExecute。这是AsyncTask的公司一般使用用途。

的差别是doInBackground经由一个后台进程完全独立的线程中运行。你有没有保证时,它将运行或完成。它不能看到UI线程。然而onPostExecute,运行在UI线程上。

什么意味的是,你的AsyncTask没有保证相同MainActivity将保持活跃。 Activies获得创建并没有接触到我们的程序员的原因不断地被破坏。无论在哪里,在这个过程中它是方向,例如发生任何变化都会破坏你的MainActivity并重建它,这意味着你传递的引用现在是空的。它可以通过第一空检查,然后在findViewById崩溃。你有没有保证外发生了什么的onCreate()。

如果你把它的内部静态类并调用onPostExecute的EditText上它应该工作的很好,因为内部类将得到重新连接到新的活动实例。

From the Logcat:

11-26 06:43:40.643: E/AndroidRuntime(1163): FATAL EXCEPTION: AsyncTask #1
...
11-26 06:43:40.643: E/AndroidRuntime(1163): java.lang.RuntimeException: An error occured while executing doInBackground()
...
11-26 06:43:40.643: E/AndroidRuntime(1163): Caused by: java.lang.NullPointerException
...
11-26 06:43:40.643: E/AndroidRuntime(1163):     at com.example.mymobiletest.SearchTask.doInBackground(SearchTask.java:134)

Line number 134 is ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);. Now the execution of this line indicates that mainActivity (it is an instance of the main activity passed to the constructor of this AsyncTask) is not null. So what else could be null at this line, which is causing the NullPointerException?

@Override
protected String doInBackground(Void... voidParameters) {
    EditText ed=null;
    if (mainActivity!=null) {
        ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);
    } else {
        return "mainActivity is the Null culprit.";
    }


EDIT:- I do think that since I am not changing the UI in doInBackground() (but only reading from the UI), so this should not be a problem. But still I tried this in onPreExecute() since onPreExecute is executed in the UI thread, but I still get the NPE on the same statement.

@Override 
    protected void onPreExecute() {
        EditText ed=null;
        if (mainActivity!=null) {
            ed = (EditText) mainActivity.findViewById(R.id.mainSearchActivity_editTextSearch);//******NPE
        } else {
            Log.i(TAG, "mainActivity is the Null culprit.");
        }
        searchQuery = ed.getText().toString();
    }

解决方案

You should make your task a static inner class unless you need to run multiple of these tasks at the same time. Then you should just separate the class entirely.

doInBackground performs a task, then returns the result to onPostExecute. This is the intended use for ASynctask's general use.

The difference is doInBackground runs in a completely separate thread via a background process. You have no guarantees when it will run or finish. It can't see the UI thread. onPostExecute however, runs on the UI thread.

What this implies is that your ASyncTask has no guarantee the same MainActivity will remain active. Activies get created and destroyed constantly for reasons not exposed to us programmers. Any change in orientation for example will destroy your MainActivity and recreate it, which means your passed reference is now empty regardless of where in the process it is. It could pass the first null check, then crash on the findViewById. You have NO guarantees what happens outside of onCreate().

If you make it an inner static class and call the edittext in onPostExecute it should work fine because the inner class will get reattached to the new activity instance.

这篇关于安卓:NullPointerException异常,这可能是空在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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