gpt4 book ai didi

android - 创建 fragment 时的NPE

转载 作者:行者123 更新时间:2023-12-01 17:07:17 25 4
gpt4 key购买 nike

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

主要 Activity :

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();
}

}

fragment 1:

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

}

}

ma​​inActivity.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)

最佳答案

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

  1. 指定布局 XML 中的所有内容
  2. 在 Java 代码中加载 Fragment

看来您将这两种范例混合在一起,这就是给您带来麻烦的原因。

为了执行布局 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"/>

关于android - 创建 fragment 时的NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61454647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com