Null指针在片段内创建片段时的异常

时间:2014-03-07 05:45:02

标签: android android-layout android-fragments android-fragmentactivity

我在片段内创建片段,但我得到了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)

1 个答案:

答案 0 :(得分:1)

更改此内容
FragmentManager fm1 = getSupportFragmentManager();

FragmentManager fm1 = getFragmentManager();