gpt4 book ai didi

java - 无法启动Activity(包含 fragment 间通信)

转载 作者:行者123 更新时间:2023-11-30 01:58:48 24 4
gpt4 key购买 nike

我基本上是 Android 开发的新手。从各种 YouTube channel 学习,当然也可以遵循官方 API 指南。

我目前正在学习 fragment ,并找到了满足类似需求的视频。 Here是它的链接。

我基本上做的是制作两个 fragment ,一个 fragment (FragmentA)包含一个简单的ListView,当我点击列表时item ,相应的内容应该显示在下面的 fragment 中,即:FragmentB

我已经按照教程中显示的所有步骤进行操作,但仍然在运行时出现以下错误:

08-04 15:19:47.038  22144-22144/com.example.abhishek.fragmentmodularui E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abhishek.fragmentmodularui/com.example.abhishek.fragmentmodularui.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2107)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4918)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
at android.app.Activity.setContentView(Activity.java:1920)
at com.example.abhishek.fragmentmodularui.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5185)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4918)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.abhishek.fragments.FragmentA: make sure class name exists, is public, and has an empty constructor that is public
at android.app.Fragment.instantiate(Fragment.java:584)
at android.app.Fragment.instantiate(Fragment.java:552)
at android.app.Activity.onCreateView(Activity.java:4828)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
            at android.app.Activity.setContentView(Activity.java:1920)
            at com.example.abhishek.fragmentmodularui.MainActivity.onCreate(MainActivity.java:16)
            at android.app.Activity.performCreate(Activity.java:5185)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4918)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.abhishek.fragments.FragmentA
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:574)
            at android.app.Fragment.instantiate(Fragment.java:552)
            at android.app.Activity.onCreateView(Activity.java:4828)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
            at android.app.Activity.setContentView(Activity.java:1920)
            at com.example.abhishek.fragmentmodularui.MainActivity.onCreate(MainActivity.java:16)
            at android.app.Activity.performCreate(Activity.java:5185)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2132)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4918)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
            at dalvik.system.NativeStart.main(Native Method)

这是我剩下的代码:

1) MainActivity.java : http://pastebin.com/6r2a15AN

2) fragment A.java:http://pastebin.com/UMDMKWVi

3) fragment B.java:http://pastebin.com/i5q1pHdq

4)Communicator.java : http://pastebin.com/fw0HbQa7

5) 字符串.xml:http://pastebin.com/Q0Qb7D6n

6)activity_main.xml:http://pastebin.com/8mw2cEHs

7) fragment _a.xml:http://pastebin.com/mq7RmSLe

8) fragment _b.xml:http://pastebin.com/Px90HWLN

9)AndroidManifest.xml:http://pastebin.com/bX71e80s

这里有什么问题?任何形式的帮助将不胜感激!

最佳答案

您的 FragmentActivity 似乎在包 com.example.abhishek.fragmentmodularui 中。在您的 activity_main.xml 中,两个 fragment 名称都指向不存在的文件:

android:name="com.example.abhishek.fragments.FragmentA"

应该是这样

   android:name="com.example.abhishek.fragmentmodularui.FragmentA"

分别

   android:name="com.example.abhishek.fragmentmodularui.FragmentB"

此外,您对 respond() 方法的实现是错误的。如下所示以供引用。

public void respond(int i) {
FragmentManager fragmentManager = getFragmentManager();
FragmentB fragmentB = new FragmentB(); //This is WRONG
fragmentB.changeData(i);
}

这里你不应该像你这样创建一个新的 fragment ,因为它们是在 Activity 启动时创建的。现在发生了什么,一个新的 fragment 被创建但没有被初始化,所以你会看到错误。您应该从 FragmentManager 获取现有 fragment 。正确的做法是

public void respond(int i) {
FragmentManager fragmentManager = getFragmentManager();
FragmentB fragmentB = (FragmentB) getFragmentManager().findFragmentById(R.id.fragment2);
fragmentB.changeData(i);
}

但您必须自己找出参数 bundlekey

关于java - 无法启动Activity(包含 fragment 间通信),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31806444/

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