我是 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)
uj5u.com熱心網友回復:
你tvDescription在DetailsFragmentxml 中,而你發現它的 id 在MainAcitvity. MainActivity 只是有FragmentContainerView,它里面沒有任何文本視圖。
您需要膨脹片段的觀點,并呼吁findViewById()在View它的回報。
更新的細節片段
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
}
}
uj5u.com熱心網友回復:
tvDescription在DetailsFragment布局中,您需要在該類的代碼中進行任何布局配置
IE
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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/323372.html
標籤:爪哇 安卓 安卓工作室 安卓布局 android-fragments
上一篇:如何使tablayout片段適合INSIDEviewpager
下一篇:如何在后臺啟動活動并立即關閉它?
