隐藏视图时崩溃

时间:2013-11-18 11:47:30

标签: android android-listview

我在列表视图的顶部有一个搜索栏,并且有一个操作按钮可以切换搜索栏的可见性......

搜索栏显示正确,但是当我隐藏它时,应用程序崩溃了。这是切换视图的代码......

/**
 * Show / Hide the search box
 */
private void toggleSearch()
{

    //in the instance there are not customers...a crash will occur when searching
    if(customers.length > 0)
    {
            if(searchVisible)//hide the search bar
            {               
                searchFrame.setVisibility(View.GONE);

                Log.i("SEARCH", "HIDDEN");

                searchVisible = false;

                Log.i("SEARCH", "BOOLEAN SET");

                resetList();

                Log.i("SEARCH", "LIST RESET?");
            }
            else//show the search bar
            {                   
                searchFrame.setVisibility(View.VISIBLE);
                searchVisible = true;
            }
        }

}

这是LogCat输出......

11-18 11:33:22.483: I/SEARCH(1591): SHOWN
11-18 11:33:22.493: D/audio_hw_primary(183): select_devices: out_snd_device(4: headphones)     in_snd_device(0: )
11-18 11:33:22.753: D/audio_hw_primary(183): out_set_parameters: enter: usecase(0: deep-buffer-playback) kvpairs: routing=4

11-18 11:33:24.473: I/SEARCH(1591): HIDDEN

11-18 11:33:24.473: I/SEARCH(1591): BOOLEAN SET

11-18 11:33:24.473: I/SEARCH(1591): LIST RESET?

11-18 11:33:24.473: D/AndroidRuntime(1591): Shutting down VM

11-18 11:33:24.473: E/AndroidRuntime(1591): FATAL EXCEPTION: main

11-18 11:33:24.473: E/AndroidRuntime(1591): Process: evans.louis.firstdroid, PID: 1591

11-18 11:33:24.473: E/AndroidRuntime(1591): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.ListView.onMeasure(ListView.java:1175)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.View.measure(View.java:16458)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.View.measure(View.java:16458)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.View.measure(View.java:16458)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-18 11:33:24.473: E/AndroidRuntime(1591):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.View.measure(View.java:16458)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.View.measure(View.java:16458)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.Choreographer.doFrame(Choreographer.java:532)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.os.Handler.handleCallback(Handler.java:733)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.os.Handler.dispatchMessage(Handler.java:95)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.os.Looper.loop(Looper.java:137)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at android.app.ActivityThread.main(ActivityThread.java:4998)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at java.lang.reflect.Method.invoke(Native Method)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)

11-18 11:33:24.473: E/AndroidRuntime(1591):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

11-18 11:33:24.483: W/ActivityManager(609):   Force finishing activity evans.louis.firstdroid/com.firsthosted.firstdroid.CustomerList

任何想法?

由于

编辑:这是请求的resetList()

/**
 * Revert the ListView to it's original order
 */
private void resetList()
{
    try
    {
        adapter = new ArrayAdapter<String>(this, R.layout.listview_container, R.id.name, customers);
        lv.setAdapter(adapter); 
    }
    catch(Exception e)
    {
        Library.errorHandler("resetList", e);
    }
}

2 个答案:

答案 0 :(得分:1)

刚刚在resetList()

中发现了我的错误

我有......

adapter = new ArrayAdapter<String>(this, R.layout.listview_container, R.id.name, customers);

应该是......

adapter = new ArrayAdapter<String>(this, R.layout.listview_text, R.id.name, customers);

答案 1 :(得分:0)

你在一个null的元素上调用setText,我假设你在if语句之外调用.setText方法。