如何在涉及片段的 Android 代码中找到空指针错误
How can I find the null pointer error in this Android Code involving Fragments
我已经尝试了 2 个小时来在我的代码中找到空指针,但没有成功。我希望你们当中有一个人比我想的要聪明得多。首先,我正在按照这里的教程进行操作:http://youtu.be/Y2liEQV3tbQ?list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo by slideNerd。
让我感到困惑的是:在水平模式下,代码可以完美执行,并且显示会更新为正确的信息。这意味着 if 语句的水平部分正在运行。但是,当它处于纵向状态并且我单击列表视图图标时,它会立即使用空指针强制关闭。错误来自以下行:
f2b.changeTextView(index);
如果我删除该行,第二个 activity 将毫无问题地打开。我完全不知道是什么给我带来了这么多麻烦。
这是我的 类:
主要活动:
public class MainActivity extends Activity implements FragmentA.Communicator{
private FragmentA f1a;
private FragmentB f2b;
private FragmentManager manager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getFragmentManager();
f1a = (FragmentA) manager.findFragmentById(R.id.fragment1a);
f1a.setCommunicator(this); //the respond method that is implemented from Fragment a
f2b = (FragmentB) manager.findFragmentById(R.id.fragment2b);
}
@Override
public void respond(int index) {
//Check if portrait mode
if (f2b != null && f2b.isVisible()){ //Horizontal
f2b.changeTextView(index);
} else { //Vertical
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
片段A:
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener {
ListView listView;
Communicator comm;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.fragment_a_list_view);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
comm.respond(position);
}
public void setCommunicator(Communicator communicator){
this.comm = communicator;
}
//Interface to allow access between fragments
public interface Communicator{
public void respond(int index);
}
}
片段 B:
public class FragmentB extends Fragment {
TextView textView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view);
}
public void changeTextView(int position){
String [] descriptions = getResources().getStringArray(R.array.descriptions);
textView.setText(descriptions[position]);
}
}
活动2:
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Intent intent = getIntent();
int index = intent.getIntExtra("index", 0);
//reference the fragment
FragmentB f2b = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2b);
if (f2b != null){
f2b.changeTextView(index);
}
}
}
这是 logcat 中的错误代码:
Caused by: java.lang.NullPointerException
at com.pgmacdesign.fragmentexamples3_flexibleui.FragmentB.changeTextView(FragmentB.java:42)
at com.pgmacdesign.fragmentexamples3_flexibleui.Activity2.onCreate(Activity2.java:25)
我已经确认索引/位置变量传递正确。如果有人对如何查找空指针错误有任何建议,我很乐意提供帮助。
在您的片段 B 中,
尝试将 textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view)
替换为 textView = (TextView) view.findViewById(R.id.fragment_b_text_view)
并将其从 onActivityCreated() 移至 onCreateView()。
希望对您有所帮助。
我认为你的问题是什么时候可以尝试找到 ID。
试试这个 textView = (TextView) view.findViewById(R.id.fragment_b_texT_view)
,其中 view
是你的 View view = inflater.inflate(R.layout.fragment_b, container, false);
。
我已经尝试了 2 个小时来在我的代码中找到空指针,但没有成功。我希望你们当中有一个人比我想的要聪明得多。首先,我正在按照这里的教程进行操作:http://youtu.be/Y2liEQV3tbQ?list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo by slideNerd。
让我感到困惑的是:在水平模式下,代码可以完美执行,并且显示会更新为正确的信息。这意味着 if 语句的水平部分正在运行。但是,当它处于纵向状态并且我单击列表视图图标时,它会立即使用空指针强制关闭。错误来自以下行:
f2b.changeTextView(index);
如果我删除该行,第二个 activity 将毫无问题地打开。我完全不知道是什么给我带来了这么多麻烦。
这是我的 类:
主要活动:
public class MainActivity extends Activity implements FragmentA.Communicator{
private FragmentA f1a;
private FragmentB f2b;
private FragmentManager manager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getFragmentManager();
f1a = (FragmentA) manager.findFragmentById(R.id.fragment1a);
f1a.setCommunicator(this); //the respond method that is implemented from Fragment a
f2b = (FragmentB) manager.findFragmentById(R.id.fragment2b);
}
@Override
public void respond(int index) {
//Check if portrait mode
if (f2b != null && f2b.isVisible()){ //Horizontal
f2b.changeTextView(index);
} else { //Vertical
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
片段A:
public class FragmentA extends Fragment implements AdapterView.OnItemClickListener {
ListView listView;
Communicator comm;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getActivity().findViewById(R.id.fragment_a_list_view);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.chapters, android.R.layout.simple_list_item_1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
comm.respond(position);
}
public void setCommunicator(Communicator communicator){
this.comm = communicator;
}
//Interface to allow access between fragments
public interface Communicator{
public void respond(int index);
}
}
片段 B:
public class FragmentB extends Fragment {
TextView textView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view);
}
public void changeTextView(int position){
String [] descriptions = getResources().getStringArray(R.array.descriptions);
textView.setText(descriptions[position]);
}
}
活动2:
public class Activity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Intent intent = getIntent();
int index = intent.getIntExtra("index", 0);
//reference the fragment
FragmentB f2b = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2b);
if (f2b != null){
f2b.changeTextView(index);
}
}
}
这是 logcat 中的错误代码:
Caused by: java.lang.NullPointerException at com.pgmacdesign.fragmentexamples3_flexibleui.FragmentB.changeTextView(FragmentB.java:42) at com.pgmacdesign.fragmentexamples3_flexibleui.Activity2.onCreate(Activity2.java:25)
我已经确认索引/位置变量传递正确。如果有人对如何查找空指针错误有任何建议,我很乐意提供帮助。
在您的片段 B 中,
尝试将 textView = (TextView) getActivity().findViewById(R.id.fragment_b_text_view)
替换为 textView = (TextView) view.findViewById(R.id.fragment_b_text_view)
并将其从 onActivityCreated() 移至 onCreateView()。
希望对您有所帮助。
我认为你的问题是什么时候可以尝试找到 ID。
试试这个 textView = (TextView) view.findViewById(R.id.fragment_b_texT_view)
,其中 view
是你的 View view = inflater.inflate(R.layout.fragment_b, container, false);
。