嗨,我有一个小型的android应用程序,它演示了一个活动中的两个片段,它应该只是一个简单的代码,但是我得到了一个空指针异常。这个应用程序只是一个一个片段的列表视图,另一个片段是一个编辑文本,它连接着主要的活动。我的书是2016年出版的,所以我认为有些东西被贬低了,这就是为什么我会有这个错误。在按下listview选项后,文本应该在另一个片段中更改。此外,我应该保存实例状态,但我还没有达到那么远。我相信错误来自听者。OnFragmentInteractionListener,它指向null,我可以注释行。谢谢。代码如下:
fragment_text.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TextFragment_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TextFragment">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/welcome" />
</FrameLayout>
TextFragment.java
package com.example.tourguide;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
* Use the {@link TextFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class TextFragment extends Fragment
{
TextView textView;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public TextFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment TextFragment.
*/
// TODO: Rename and change types and number of parameters
public static TextFragment newInstance(String param1, String param2) {
TextFragment fragment = new TextFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_text, container, false);
textView = (TextView)getActivity().findViewById(R.id.textView1);
return view;
}
public void updateTextView(int index)
{
if(index==0)
{
textView.setText(getResources().getString(R.string.set_Richmond));
}
else if(index==1)
{
textView.setText(getResources().getString(R.string.set_Vancouver));
}
else if(index==2)
{
textView.setText(getResources().getString(R.string.set_Toronto));
}
else if(index==3)
{
textView.setText(getResources().getString(R.string.set_New_West));
}
else if(index==4)
{
textView.setText(getResources().getString(R.string.set_Montreal));
}
else if(index==5)
{
textView.setText(getResources().getString(R.string.set_Aldergrove));
}
}
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction(int index);
}
}
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ListFragment_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListFragment">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="32dp" />
</FrameLayout>
ListFragment.java
package com.example.tourguide;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ListFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ListFragment extends Fragment implements AdapterView.OnItemClickListener
{
ListView listView;
String[] array;
String listStr;
OnFragmentInteractionListener listener;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public ListFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ListFragment.
*/
// TODO: Rename and change types and number of parameters
public static ListFragment newInstance(String param1, String param2) {
ListFragment fragment = new ListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_list, container, false);
listView = view.findViewById(R.id.listView1);
array = getResources().getStringArray(R.array.string_array_cities);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
//listener= (MainActivity)getActivity();
listView.setOnItemClickListener(this::onItemClick);
return view;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long id)
{
//if(listener != null)
//{
listener.onFragmentInteraction(index); //this is what points to null (the listener)
//}
}
public interface OnFragmentInteractionListener
{
public void onFragmentInteraction(int index);
}
}
activity_main.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_1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView1_list"
android:name="com.example.tourguide.ListFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2_text"
android:name="com.example.tourguide.TextFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="@layout/fragment_text" />
</LinearLayout>
MainActivity.java
package com.example.tourguide;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements TextFragment.OnFragmentInteractionListener, ListFragment.OnFragmentInteractionListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onFragmentInteraction(int index)
{
FragmentManager manager = getSupportFragmentManager();
TextFragment textFragment = (TextFragment)manager.findFragmentById(R.id.fragmentContainerView2_text);
textFragment.updateTextView(index);
}
}
strings.xml
<resources>
<string name="app_name">Tour Guide</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="text_box">default</string>
<string name="welcome">Welcome to the Tour Guide App</string>
<string name="set_Richmond">Richmond is a quite city with many nice hidden scenic places. A small town named Steveston is found on the south west part
of the city and is well known for having good seafood and nice places to visit like Garry Point and the west and south dykes. </string>
<string name="set_Vancouver">Vancouver is one of Canadas biggest cities and has a lot to offer like the malls and many towers, beaches, parks.</string>
<string name="set_Toronto">Toronto is Canadas biggest city on the east coast and home to the TSX and Maple Leafs.</string>
<string name="set_New_West">New West is a city east of Vancouver but close by and has nice parks and ice rinks like Moody Park and Queens Park.
Queens Park home of the New West Royals.</string>
<string name="set_Montreal">Monteal is famous for their smoked meat and they speak french.</string>
<string name="set_Aldergrove">Aldergrove is a quite town and is east of Langley not too far and is a nice place.</string>
<string-array name="string_array_cities">
<item>Richmond</item>
<item>Vancouver</item>
<item>Toronto</item>
<item>New West</item>
<item>Montreal</item>
<item>Aldergrove</item>
</string-array>
</resources>发布于 2022-01-29 09:04:57
如果要在片段中使用findById,则必须在它们前面添加“视图”,将它们添加到您返回的视图中。
在您的onCreateView更改:
textView = (TextView)getActivity().findViewById(R.id.textView1);致:
textView = view.findViewById(R.id.textView1);https://stackoverflow.com/questions/70903532
复制相似问题