android-fragments 如何解决Android中的空指针异常?[duplicate]

vuv7lop3  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(218)

此问题在此处已有答案

Avoiding NullPointerException in Java(66个答案)
What is a NullPointerException, and how do I fix it?(12个答案)
四个月前关门了。
我正在处理片段。我已经创建了一个列表片段和一个细节片段。当我点击列表中的一个项目时,应用程序崩溃,并给出以下空指针异常。
尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”
我尝试过类似问题的解决方案,但没有一个对我有效。
以下是我的主要活动:

public class MainActivity extends AppCompatActivity implements ListFrag.ItemSelected {

    TextView tvDescription;
    ArrayList<String> descriptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       tvDescription = (TextView) findViewById(R.id.tvDescription);

        descriptions = new ArrayList<>();
        descriptions.add("Description for item 1");
        descriptions.add("Description for item 2");
        descriptions.add("Description for item 3");

    }

    @Override
    public void onItemSelected(int index) {

        tvDescription.setText(descriptions.get(index));
    }
}

列表片段:

public class ListFrag extends ListFragment {

    ItemSelected activity;

    public  interface ItemSelected
    {
        void onItemSelected(int index);
    }

    public ListFrag() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        activity = (ItemSelected) context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayList<String> data = new ArrayList<>();
        data.add("1.This is item 1");
        data.add("2.This is item 2");
        data.add("3.This is item 3");

        setListAdapter(new ArrayAdapter<>( getActivity() , android.R.layout.simple_list_item_1,data));
    }

    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {

        activity.onItemSelected(position);
    } }

细节片段:

public class DetailFrag extends Fragment {


    public DetailFrag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }
}

活动主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/listFrag"
        android:name="com.example.fragmentspart1.ListFrag"
        android:layout_width="8dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_list" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/detailFrag"
        android:name="com.example.fragmentspart1.DetailFrag"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        tools:layout="@layout/fragment_detail" />

</LinearLayout>

片段列表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFFFF"
    tools:context=".ListFrag">

    <ListView
        android:id="@+id/lvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

fragment_detail布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/design_default_color_secondary"
    tools:context=".DetailFrag">

    <TextView
        android:id="@+id/tvDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/textview"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>
nmpmafwu

nmpmafwu1#

(TextView) findViewById(R.id.tvDescription);

在MainActivity中返回空值,因为TextView tvDescription采用fragment_detail布局。
您必须将此行移动到DetailFragment的onViewCreated(),并且在onItemSelected()中,您必须创建DetailFragmet的新示例,并将数据作为参数传递给DetailFragment示例。
启动DetailFragment后,您可以在DetailFragment中执行以下操作:

override onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       tvDescription = (TextView) findViewById(R.id.tvDescription);
       // get your list item data from arguments
       tvDescription.setText(descriptions.get(index));
    }

相关问题