创建片段时的NPE

时间:2015-01-23 14:32:10

标签: android android-fragments

我正在使用Fragment创建一个关于sensorEventlitener的示例。 mainactivity托管fragment,如下面的代码所示,但在运行时我收到以下发布的logcat错误。

MainActivity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    initViews();
}

private void initViews() {
    // TODO Auto-generated method stub

    View frag1 = findViewById(R.id.frag_1);

    FragmentManager fMgr = getFragmentManager();
    FragmentTransaction fragmentTransaction = fMgr.beginTransaction();
    fragmentTransaction.replace(R.id.frag_1, new Frag1());
    fragmentTransaction.commit();
}

}

Frag1

public class Frag1 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
TextView tvAccX;
TextView tvAccY;
TextView tvAccZ;
Button btnSend;

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.frag_1, container, false);

    tvAccX = (TextView) view.findViewById(R.id.tv_accX_value);
    tvAccY = (TextView) view.findViewById(R.id.tv_accY_value);
    tvAccZ = (TextView) view.findViewById(R.id.tv_accZ_value);
    btnSend = (Button) view.findViewById(R.id.btn_send);

    return view;
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_FASTEST);
}

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        showAccReadings(event);
        break;

    default:
        break;
    }
}

private void showAccReadings(SensorEvent event) {
    // TODO Auto-generated method stub
    float[] values = event.values;

    float x = values[0];
    float y = values[1];
    float z = values[2];

    tvAccX.setText(String.valueOf(x));
    tvAccY.setText(String.valueOf(y));
    tvAccZ.setText(String.valueOf(z));
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

}

mainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">

<fragment
    android:id="@+id/frag_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

<fragment
    android:id="@+id/frag_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/frag_1"/>

</RelativeLayout>

Frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView 
    android:id="@+id/tv_accX_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:text="Acc[X]: "/>
<TextView 
    android:id="@+id/tv_accX_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_toRightOf="@+id/tv_accX_label"/>

<TextView 
    android:id="@+id/tv_accY_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tv_accX_label"
    android:text="Acc[Y]: "/>
<TextView 
    android:id="@+id/tv_accY_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_toRightOf="@+id/tv_accY_label"/>

<TextView 
    android:id="@+id/tv_accZ_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/tv_accY_label"
    android:text="Acc[Z]: "/>
<TextView 
    android:id="@+id/tv_accZ_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_toRightOf="@+id/tv_accZ_label"/>

<Button 
    android:id="@+id/btn_send"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_accZ_label"
    android:gravity="center_vertical|center_horizontal"
    android:text="send to frag_2"/>
</RelativeLayout>

logcat的

01-23 15:25:11.200: E/AndroidRuntime(7195): Process: com.example.fragmentcommunication_00, PID: 7195
01-23 15:25:11.200: E/AndroidRuntime(7195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentcommunication_00/com.example.fragmentcommunication_00.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread.access$800(ActivityThread.java:157)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.os.Looper.loop(Looper.java:157)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread.main(ActivityThread.java:5293)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.reflect.Method.invoke(Method.java:515)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at dalvik.system.NativeStart.main(Native Method)
01-23 15:25:11.200: E/AndroidRuntime(7195): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-2    3 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:340)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Activity.setContentView(Activity.java:1973)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at com.example.fragmentcommunication_00.MainActivity.onCreate(MainActivity.java:17)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Activity.performCreate(Activity.java:5389)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
01-23 15:25:11.200: E/AndroidRuntime(7195):     ... 11 more
01-23 15:25:11.200: E/AndroidRuntime(7195): Caused by: java.lang.NullPointerException: name == null
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Fragment.instantiate(Fragment.java:583)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Fragment.instantiate(Fragment.java:561)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.app.Activity.onCreateView(Activity.java:4927)
01-23 15:25:11.200: E/AndroidRuntime(7195):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)

更新

Frag1.xml

 <fragment
    android:name="com.example.fragmentcommunication_00.Frag1"
    android:id="@+id/frag_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

Logact

01-23 15:37:42.980: E/AndroidRuntime(8267): FATAL EXCEPTION: main
01-23 15:37:42.980: E/AndroidRuntime(8267): Process: com.example.fragmentcommunication_00, PID: 8267
01-23 15:37:42.980: E/AndroidRuntime(8267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentcommunication_00/com.example.fragmentcommunication_00.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread.access$800(ActivityThread.java:157)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.os.Handler.dispatchMessage(Handler.java:102)
    01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.os.Looper.loop(Looper.java:157)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread.main(ActivityThread.java:5293)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.reflect.Method.invoke(Method.java:515)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at dalvik.system.NativeStart.main(Native Method)
01-23 15:37:42.980: E/AndroidRuntime(8267): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:340)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Activity.setContentView(Activity.java:1973)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at com.example.fragmentcommunication_00.MainActivity.onCreate(MainActivity.java:17)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Activity.performCreate(Activity.java:5389)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
01-23 15:37:42.980: E/AndroidRuntime(8267):     ... 11 more
01-23 15:37:42.980: E/AndroidRuntime(8267): Caused by: java.lang.NullPointerException: name == null
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Fragment.instantiate(Fragment.java:583)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Fragment.instantiate(Fragment.java:561)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.app.Activity.onCreateView(Activity.java:4927)
01-23 15:37:42.980: E/AndroidRuntime(8267):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)

2 个答案:

答案 0 :(得分:2)

您的活动布局文件中的name标记中似乎缺少fragment属性。基本上有两种方法可以将Fragment扩展到视图层次结构中:

  1. 指定布局XML中的所有内容
  2. 在Java代码中加载Fragment
  3. 看来你把这两种范式混合在了一起,这就是造成麻烦的原因。

    为了完成布局XML中的所有操作,您必须指定name属性。所以,像这样:

    <fragment
        android:name="com.my.package.for.Frag1"
        android:id="@+id/frag_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
    

    然后,您可以从initViews()方法中删除代码,因为不再需要它。

    相反,如果您希望在代码中执行所有操作,则必须更改布局以指定Fragment将使用的空容器。例如,使用FrameLayout而不是fragment标记,如下所示:

    <FrameLayout
        android:id="@+id/frag_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
    

答案 1 :(得分:1)

您要在xml中声明<fragment标记

   <fragment
    android:id="@+id/frag_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/frag_1"/>

没有指定android:name=属性,该属性告诉系统它必须实例化哪个片段。它必须包含要实例化的片段子类的完整限定路径。 E.g

android:name="path.to.Frag1"