English 中文(简体)
和机器人手持器
原标题:Android Handler NullPointerException
  1. The Activity creates a new Thread.
  2. The run() method gets an object (vector) via input stream.
  3. Now I want to display the contents of this Vector on a ListView called direc.
  4. That s all...but its not working and throws an Exception.

代码:

public class FileExplorerActivity extends Activity implements Runnable
{
    public static final String TAG="ricky";
    Vector<String> dirs = new Vector<String>();
    ListView direc;
    static ArrayAdapter<String> arrayAdapter;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fileexplorer);
        direc = (ListView) findViewById(R.id.directoriesss);
        handler = new Handler();
        Thread thread = new Thread(this);
        thread.start();
    }

    public FileExplorerActivity() 
    {

    }

    public void run()
    {
        Log.d(TAG, "FileExplorer: Thread started");
        try
        {
            Log.d(TAG, "FileExplorer: reading data");
            dirs = (Vector<String>) ConnectionThread.din.readObject();
            Log.d(TAG, "FileExplorer: read data");
            arrayAdapter = new ArrayAdapter<String>(this, 
                                     android.R.layout.activity_list_item,
                                     android.R.id.text2 , dirs);            
            Log.d(TAG, "FileExplorer: ArrayAdapter created");

            handler.post( new Runnable() {
                public void run()
                {
                    Log.d(TAG, "FileExplorer: In handler Thread");
                    direc.setAdapter(arrayAdapter);
                    Log.d(TAG, "FileExplorer: Updated UI");
                }
            });

            Log.d(TAG, "FileExplorer: Directory set");
        }catch (Exception e) {
            Log.d(TAG, "FileExplorerActivity: Exception:"+e.getMessage());
        }
    }
}

以下是Logcat必须说的:

> 1. 05-24 20:14:46.601  7759  7834 D ricky   : FileExplorer: Thread started
> 2. 05-24 20:14:46.601  7759  7834 D ricky   : FileExplorer: reading data
> 3. 05-24 20:14:46.648  7759  7834 D ricky   : FileExplorer: read data
> 4. 05-24 20:14:46.648  7759  7834 D ricky   : FileExplorer: ArrayAdapter created
> 5. 05-24 20:14:46.648  7759  7834 D ricky   : FileExplorer: Directory set
> 6. 05-24 20:14:46.656  7759  7759 D ricky   : FileExplorer: In handler Thread
> 7. 05-24 20:14:46.656  7759  7759 D ricky   : FileExplorer: Updated UI
> 8. 05-24 20:14:46.664  7759  7759 D AndroidRuntime: Shutting down VM
> 9. 05-24 20:14:46.664  7759  7759 W dalvikvm: threadid=1: thread exiting with uncaught 10. exception (group=0x4001e560)
> 11. 05-24 20:14:46.687  7759  7759 E AndroidRuntime: FATAL EXCEPTION: main
> 12. 05-24 20:14:46.687  7759  7759 E AndroidRuntime: java.lang.NullPointerException
> 13. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
> 14. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
> 15. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.AbsListView.obtainView(AbsListView.java:1456)
> 16. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.ListView.measureHeightOfChildren(ListView.java :1291)
> 17. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.ListView.onMeasure(ListView.java:1202)
> 18. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.View.measure(View.java:8355)
> 19. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279)
> 20. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
> 21. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.LinearLayout.measureHorizontal(LinearLayout.ja va:701)
> 22. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
> 23. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.View.measure(View.java:8355)
> 24. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279)
> 25. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
> 26. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.View.measure(View.java:8355)
> 27. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.LinearLayout.measureVertical(LinearLayout.java :531)
> 28. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
> 29. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.View.measure(View.java:8355)
> 30. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :3279)
> 31. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
> 32. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.View.measure(View.java:8355)
> 33. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.ViewRoot.performTraversals(ViewRoot.java:882)
> 34. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.view.ViewRoot.handleMessage(ViewRoot.java:1917)
> 35. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
> 36. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:130)
> 37. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:3859)
> 38. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at java.lang.reflect.Method.invokeNative(Native Method)
> 39. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Method.java:507)
> 40. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> 41. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
> 42. 05-24 20:14:46.687  7759  7759 E AndroidRuntime:        at dalvik.system.NativeStart.main(Native Method)
最佳回答

请注意 < code> NullPointerExpendion 发生时的线条:

android.widget. ArrayAdapter. create ViewFromResources(ArrayAdapter.java:353) (ArrayAdapter.java:353)


这是因为android.R.layout.activity_list_item 不含android.R.id.text2

您可以使用 android.R.id.text1 代替或用 semply_list_item_2 取代行布局。

我建议先建立你自己的行布局 以避免发生这种情况

问题回答

移移此移

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item,
        android.R.id.text2 , dirs);

运行 () 方法, 以便它运行在 UI 线索上。 您不应该在 UI 线索之外创建一个 ArrayAdapter 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签