删除视图后的空对象引用

时间:2016-12-31 12:39:31

标签: android nullpointerexception

所以我试图根据在微调器中选择的项目来显示certien。但当我从原始布局中删除一个视图替换它然后尝试以任何方式引用它,它给我一个空对象引用。

代码:

if(subjectSpinner.getSelectedItem().toString().equals("All")){

                allSelected();

            } else {

                if(taskList == null){
                    Log.i("SQL", "task list id null");
                    taskList = (RecyclerView) findViewById(R.id.tasks_list);
                }

//error points at this line
                if(taskList.getParent() == null){

                    TextView textView = (TextView) findViewById(R.id.textView10);

                    baseView.removeAllViews();

                    baseView.addView(appBarLayout);
                    baseView.addView(subjectSpinner);
                    baseView.addView(taskList);
                    baseView.addView(textView);

                }

allSelected()方法:

void allSelected (){
    subjectSpinner = (Spinner) findViewById(R.id.tasks_list_spinner);
    taskList = (RecyclerView) findViewById(R.id.tasks_list);
    baseView = (RelativeLayout) findViewById(R.id.activity_view_tasks);

    baseView.removeView(taskList);

    ScrollView           scrollView    = new ScrollView(getApplicationContext());
    LinearLayout         linearLayout  = new LinearLayout(getApplicationContext());
    List <TextView>      subjectText   = new ArrayList <>();
    List <RecyclerView>  recyclerViews = new ArrayList <>();
    List <CustomAdaptor> adaptor       = new ArrayList <>();

    RelativeLayout.LayoutParams scrollViewParams =
            new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                            ViewGroup.LayoutParams.MATCH_PARENT);
    scrollViewParams.addRule(RelativeLayout.BELOW, R.id.textView10);

    RelativeLayout.LayoutParams linearLayoutParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                                            RelativeLayout.LayoutParams.WRAP_CONTENT);

PS。这些不是完整的代码,但我在这些片段之外的taskList recyclerView上没有做任何其他事情。

logcat的:

 Process: com.example.user.timetable_test, PID: 12685
 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.support.v7.widget.RecyclerView.getParent()' on a null object reference
     at com.example.user.timetable_test.ViewTasks$1.onItemSelected(ViewTasks.java:225)
     at android.widget.AdapterView.fireOnSelected(AdapterView.java:1165)
     at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1154)
     at android.widget.AdapterView.access$300(AdapterView.java:59)
     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1121)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:158)
     at android.app.ActivityThread.main(ActivityThread.java:7225)

                                                                                     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_view_tasks"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.timetable_test.ViewTasks"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_viewTasks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar3"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" android:layout_weight="1"/>
    </android.support.design.widget.AppBarLayout>

    <Spinner
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/tasks_list_spinner"
        android:layout_below="@+id/appbar_viewTasks" android:layout_alignParentEnd="true"
        android:layout_marginEnd="@dimen/activity_vertical_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"/>

    <TextView
        android:text="@string/subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/textView10"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:layout_below="@+id/appbar_viewTasks" android:layout_alignParentStart="true"
        android:layout_marginStart="@dimen/activity_vertical_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent" android:id="@+id/tasks_list"
        android:fitsSystemWindows="true" android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_vertical_margin"
        android:layout_below="@+id/textView10"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"/>
</RelativeLayout>

有人知道为什么我会收到此错误以及如何解决此问题?

BTW这不是什么是NullPointerException的副本,我该如何修复它?。是的,它是关于空指针异常,但它不要求什么是空指针异常,而是我是如何在此代码中获得空指针异常的? < / p>

由于我正在编辑问题并且无法发布答案,因此我要说我通过不删除视图来修复错误,而是使它们不可见。这是一个解决方案而不是修复。如果你有类似的问题,希望这有帮助:)

2 个答案:

答案 0 :(得分:0)

问题是,即使tasklist的空检查失败,您仍然会为其调用getParent。您应该将else部分的其余代码包含在if (tasklist ==null)部分中,如下所示:

if(subjectSpinner.getSelectedItem().toString().equals("All")){

                allSelected();

            } else {

                if(taskList == null){
                    Log.i("SQL", "task list id null");
                    taskList = (RecyclerView) findViewById(R.id.tasks_list);
                }

//add else here
               else{
                if(taskList.getParent() == null){

                    TextView textView = (TextView) findViewById(R.id.textView10);

                    baseView.removeAllViews();

                    baseView.addView(appBarLayout);
                    baseView.addView(subjectSpinner);
                    baseView.addView(taskList);
                    baseView.addView(textView);

                }
        }

答案 1 :(得分:0)

错误代码显示:

Attempt to invoke virtual method 'android.view.ViewParent android.support.v7.widget.RecyclerView.getParent()' on a null object reference

这意味着taskListnull

您的代码中可能存在错误:

  • 检查setContentView(R.layout.your_layout)或膨胀的布局 - 是your_layout.xml中的tasks_list id还是inflated布局?