空指针在片段内创建片段时出现异常
问题描述:
我在片段内创建片段,但我得到了NullPointerException
。我不知道为什么相同的代码在扩展FragmentActivity
的类中工作,但它在扩展Fragment
的类中不起作用。以下是我的代码,空指针在片段内创建片段时出现异常
public class SecondLevel extends Fragment implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button btn1, btn2, btn3, btn4;
View view = inflater.inflate(R.layout.secondlevel, container, false);
btn1 = (Button) view.findViewById(R.id.button11);
btn1.setOnClickListener(this);
...
...
...
return view;
}
@Override
public void onClick(View view) {
Fragment fr1 = null;
switch (view.getId()) {
case R.id.button11:
Toast.makeText(this.getActivity(), "Button 1 is clicked!",
Toast.LENGTH_LONG).show();
fr1 = new ThirdFragment();
FragmentManager fm1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm1.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr1);
fragmentTransaction.commit();
.....
现在在ThirdFragment我在做什么如下:
package com.javacodegeeks.android.fragmentstest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("F1", "F1");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.thirdlevel, container, false);
}
}
请注意,我已经进口的相同的支持库,所以它不是这个问题。
它我的XML第三层子:
<Button
android:id="@+id/buttonlevel3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
它我的第二级别的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/laSecond"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level 1" />
<Button
android:id="@+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level 2" />
<Button
android:id="@+id/button22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<TextView
android:id="@+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level 3" />
<Button
android:id="@+id/button33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<TextView
android:id="@+id/textView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Level 4" />
<Button
android:id="@+id/button44"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
</LinearLayout>
logcat的:
E/AndroidRuntime(19842): FATAL EXCEPTION: main
03-07 10:58:09.138: E/AndroidRuntime(19842): java.lang.NullPointerException
03-07 10:58:09.138: E/AndroidRuntime(19842):at com.javacodegeeks.android.fragmentstest.SecondLevel.onClick(SecondLevel.java:50)
03-07 10:58:09.138: E/AndroidRuntime(19842): at android.view.View.performClick(View.java:3549)
E/AndroidRuntime(19842): at android.view.View$PerformClick.run(View.java:14393)
E/AndroidRuntime(19842):at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(19842):at android.os.Handler.dispatchMessage(Handler.java:92)
答
改变这种从
FragmentManager fm1 = getSupportFragmentManager();
到
FragmentManager fm1 = getFragmentManager();
+1
谢谢,虽然我第一次尝试,但我不知道它不会工作之前对我来说,现在它的工作非常感谢您的帮助。 –
+1
你好,来吧..很高兴帮助你! – Piyush
请发表您的logcat的错误。 –
你还可以发布'second_level.xml'布局吗?我有一种感觉,它不包含'fragment_place'元素.. –
尝试fragmentTransaction.add而不是fragmentTransaction.replace。 – Vijju