android-fragments Android Studio中片段中的TextView返回空值

ecbunoof  于 2022-11-14  发布在  Android
关注(0)|答案(3)|浏览(174)

我是Android Studio的新手..我在Fragment内的TextView上遇到了空指针异常错误,请帮助..这是我的代码。这只是两个片段之间的简单接口。当我单击ListView Fragment中的某个项目时,我遇到了空指针异常错误。请确保DetailsFragment中的TextView没有分配或指向它的源。请帮助解决此问题。

public class MainActivity extends AppCompatActivity implements list_Fragment.ItemSelected {

    ArrayList<String> Descriptions = new ArrayList<String>();;
    TextView tvDescription;

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

        Descriptions.add("Description for Item 1");
        Descriptions.add("Description for Item 2");
        Descriptions.add("Description for Item 3");
        Descriptions.add("Description for Item 4");
        tvDescription = findViewById(R.id.tvDescription);
    }

    @Override
    public void onItemSelected(int index) {
        tvDescription.setText(Descriptions.get(index));
    }}

列表片段

public class list_Fragment extends ListFragment {

    ArrayList<String> Data = new ArrayList<String>();

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

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

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity=(ItemSelected) context;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Data.add("Item 1");
        Data.add("Item 2");
        Data.add("Item 3");
        Data.add("Item 4");
        setListAdapter(new ArrayAdapter<String>(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 DetailsFragment extends Fragment {

    public DetailsFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

主XML文件

<?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:id="@+id/ll_Horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/listfragmentView"
        android:name="com.example.fragmentcheck.list_Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/holo_blue_dark"
        tools:layout="@layout/fragment_list_" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/detailsfragmentView"
        android:name="com.example.fragmentcheck.DetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/purple_200"
        tools:layout="@layout/fragment_details" />
</LinearLayout>

列表片段XML

<?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:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_primary_variant"
    android:orientation="vertical"
    tools:context=".list_Fragment">

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

详细信息片段XML

<?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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/design_default_color_on_secondary"
    android:orientation="vertical"
    tools:context=".DetailsFragment">

    <TextView
        android:id="@+id/tvDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</LinearLayout>

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fragmentcheck, PID: 28112
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object r*emphasized text*eference
        at com.example.fragmentcheck.MainActivity.onItemSelected(MainActivity.java:34)
        at com.example.fragmentcheck.list_Fragment.onListItemClick(list_Fragment.java:54)
llew8vvj

llew8vvj1#

tvDescriptionDetailsFragment布局中,您需要在该类的代码中进行任何布局配置
也就是说

class DetailsFragment extends Fragment {

...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_details, container, false)
          tvDescription = view.findViewById(R.id.tvDescription);
          tvDescription.setText("some text");
       
        return view
    }

}
u5rb5r59

u5rb5r592#

你在DetailsFragment xml中有tvDescription,而你在MainAcitvity中找到它的id。MainActivity只有FragmentContainerView,它里面没有任何文本视图。
您需要膨胀Fragment的视图,并在它返回的View上调用findViewById()

更新的详细信息片段

public class DetailsFragment extends Fragment {

    TextView tvDescription;
    public DetailsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_details, container, false)
          tvDescription = view.findViewById(R.id.tvDescription);
          tvDescription.setText("some text");
       
        return view
    }
}
j2cgzkjk

j2cgzkjk3#

同时更改:androidx.fragment.app.FragmentContainerView
至:片段
在main_activity xml中

相关问题