android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment

huangapple go评论108阅读模式
英文:

android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment

问题

错误分析与建议:

根据提供的信息,您的 Android 应用在首页出现错误,主要是由于 XML 布局文件中的某个片段(<fragment>)引发的问题,导致应用崩溃。同时,还提供了 Java 文件中的代码,这些代码负责处理按钮点击事件和一些逻辑。以下是关于问题的分析和建议:

  1. XML 布局错误
    在您提供的 XML 布局文件中,第 38 行包含一个 <fragment> 元素,而根据错误堆栈信息,这是导致应用崩溃的根本原因之一。具体的错误是 Error inflating class fragment。这种情况通常与 <fragment> 元素中指定的片段类名有关。

  2. 解决方法
    您可以尝试以下解决方法来处理这个问题:

    • 确保 tools:context=".HomeActivity" 属性指向正确的活动类名。
    • 检查 android:name 属性是否指向有效的片段类名。
    • 确保片段类存在并正确引用。
  3. 其他建议
    除了 XML 布局问题外,您的 Java 代码看起来还不错。如果您的应用仍然存在问题,请确保以下几点:

    • 检查 MapsActivity 类是否存在,并且在 launchactivity 方法中正确引用。
    • 如果涉及网络请求或 Firebase 等其他功能,确保您已正确配置这些部分。

请根据上述建议检查和调整您的代码,特别是修复 XML 布局文件中的问题,这应该能够解决您遇到的应用崩溃问题。如果问题仍然存在,请提供更多的错误信息或日志,以便更详细地分析和提供帮助。

英文:

Android terminal
I getting errors in homepage of my new app of helpline,I tried to check all but still not running please check and comment accordingly Please review

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.googlemaps, PID: 18785
  3. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.HomeActivity}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
  4. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
  5. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
  6. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
  7. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
  8. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
  9. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
  10. at android.os.Handler.dispatchMessage(Handler.java:106)
  11. at android.os.Looper.loop(Looper.java:214)
  12. at android.app.ActivityThread.main(ActivityThread.java:7078)
  13. at java.lang.reflect.Method.invoke(Native Method)
  14. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
  15. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)
  16. Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class fragment
  17. Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class fragment
  18. Caused by: java.lang.NullPointerException
  19. at java.lang.VMClassLoader.findLoadedClass(Native Method)
  20. at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
  21. at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
  22. at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
  23. at android.app.Fragment.instantiate(Fragment.java:524)
  24. at android.app.FragmentContainer.instantiate(FragmentContainer.java:53)
  25. at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3551)
  26. at android.app.FragmentController.onCreateView(FragmentController.java:103)
  27. at android.app.Activity.onCreateView(Activity.java:6445)
  28. at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
  29. at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:819)
  30. at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
  31. at android.view.LayoutInflater.rInflate(LayoutInflater.java:902)
  32. at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:863)
  33. at android.view.LayoutInflater.inflate(LayoutInflater.java:554)
  34. at android.view.LayoutInflater.inflate(LayoutInflater.java:461)
  35. at android.view.LayoutInflater.inflate(LayoutInflater.java:383)
  36. at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
  37. at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
  38. at com.example.googlemaps.HomeActivity.onCreate(HomeActivity.java:24)
  39. at android.app.Activity.performCreate(Activity.java:7327)
  40. at android.app.Activity.performCreate(Activity.java:7318)
  41. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
  42. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
  43. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
  44. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
  45. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
  46. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
  47. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
  48. at android.os.Handler.dispatchMessage(Handler.java:106)
  49. at android.os.Looper.loop(Looper.java:214)
  50. at android.app.ActivityThread.main(ActivityThread.java:7078)
  51. at java.lang.reflect.Method.invoke(Native Method)
  52. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
  53. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

xml file

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;HomeActivity&quot;&gt;
  8. &lt;TextView
  9. android:id=&quot;@+id/textView2&quot;
  10. android:layout_width=&quot;wrap_content&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:layout_marginLeft=&quot;125dp&quot;
  13. android:layout_marginTop=&quot;5dp&quot;
  14. android:text=&quot;Welcome! You are logged in&quot;
  15. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  16. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  17. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  18. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  19. tools:ignore=&quot;HardcodedText&quot;
  20. android:layout_marginStart=&quot;125dp&quot; /&gt;
  21. &lt;TextView
  22. android:id=&quot;@+id/texttitle&quot;
  23. android:layout_width=&quot;wrap_content&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:layout_marginLeft=&quot;125dp&quot;
  26. android:layout_marginTop=&quot;25dp&quot;
  27. android:text=&quot;24x7 SECURITY HELPLINE&quot;
  28. android:textSize=&quot;25sp&quot;
  29. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  30. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  31. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  32. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  33. tools:ignore=&quot;HardcodedText&quot;
  34. android:layout_marginStart=&quot;50dp&quot; /&gt;
  35. &lt;fragment
  36. android:id=&quot;@+id/home&quot;
  37. android:layout_height=&quot;match_parent&quot;
  38. android:layout_width=&quot;140dp&quot;
  39. android:layout_marginTop=&quot;80dp&quot;
  40. tools:context=&quot;.HomeActivity&quot;
  41. android:background=&quot;#FA8072&quot; /&gt;
  42. &lt;ImageButton
  43. android:id=&quot;@+id/imageButton&quot;
  44. android:layout_width=&quot;140dp&quot;
  45. android:layout_height=&quot;130dp&quot;
  46. android:layout_marginTop=&quot;80dp&quot;
  47. tools:src=&quot;@tools:sample/avatars&quot;
  48. tools:ignore=&quot;HardcodedText&quot;
  49. android:contentDescription=&quot;Profile Pic&quot;/&gt;
  50. &lt;Button
  51. android:id=&quot;@+id/profile&quot;
  52. android:layout_width=&quot;140dp&quot;
  53. android:layout_height=&quot;wrap_content&quot;
  54. android:layout_marginTop=&quot;210dp&quot;
  55. android:text=&quot;Your Profile&quot;
  56. tools:ignore=&quot;HardcodedText&quot;/&gt;
  57. &lt;Button
  58. android:id=&quot;@+id/complain&quot;
  59. android:layout_width=&quot;140dp&quot;
  60. android:layout_height=&quot;wrap_content&quot;
  61. android:layout_marginTop=&quot;250dp&quot;
  62. android:text=&quot;Complain&quot;
  63. tools:ignore=&quot;HardcodedText&quot;/&gt;
  64. &lt;Button
  65. android:id=&quot;@+id/tips&quot;
  66. android:layout_width=&quot;140dp&quot;
  67. android:layout_height=&quot;wrap_content&quot;
  68. android:layout_marginTop=&quot;290dp&quot;
  69. android:text=&quot;Safety Tips&quot;
  70. tools:ignore=&quot;HardcodedText&quot;/&gt;
  71. &lt;Button
  72. android:id=&quot;@+id/alerts&quot;
  73. android:layout_width=&quot;140dp&quot;
  74. android:layout_height=&quot;wrap_content&quot;
  75. android:layout_marginTop=&quot;330dp&quot;
  76. android:text=&quot;Alerts&quot;
  77. tools:ignore=&quot;HardcodedText&quot;/&gt;
  78. &lt;Button
  79. android:id=&quot;@+id/emergency&quot;
  80. android:layout_width=&quot;140dp&quot;
  81. android:layout_height=&quot;wrap_content&quot;
  82. android:layout_marginTop=&quot;370dp&quot;
  83. android:text=&quot;emergency&quot;
  84. tools:ignore=&quot;HardcodedText&quot;/&gt;
  85. &lt;Button
  86. android:id=&quot;@+id/nearby&quot;
  87. android:layout_width=&quot;140dp&quot;
  88. android:layout_height=&quot;wrap_content&quot;
  89. android:layout_marginTop=&quot;410dp&quot;
  90. android:text=&quot;NearBy&quot;
  91. tools:ignore=&quot;HardcodedText&quot;/&gt;
  92. &lt;Button
  93. android:id=&quot;@+id/volunteer&quot;
  94. android:layout_width=&quot;140dp&quot;
  95. android:layout_height=&quot;wrap_content&quot;
  96. android:layout_marginTop=&quot;450dp&quot;
  97. android:text=&quot;Volunteer&quot;
  98. tools:ignore=&quot;HardcodedText&quot;/&gt;
  99. &lt;Button
  100. android:id=&quot;@+id/logout&quot;
  101. android:layout_width=&quot;140dp&quot;
  102. android:layout_height=&quot;wrap_content&quot;
  103. android:layout_marginLeft=&quot;1dp&quot;
  104. android:layout_marginTop=&quot;490dp&quot;
  105. android:text=&quot;Logout&quot;
  106. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  107. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  108. app:layout_constraintTop_toBottomOf=&quot;@+id/textView2&quot;
  109. tools:ignore=&quot;HardcodedText&quot;
  110. android:layout_marginStart=&quot;1dp&quot; /&gt;
  111. &lt;Button
  112. android:id=&quot;@+id/btn&quot;
  113. android:layout_width=&quot;wrap_content&quot;
  114. android:layout_height=&quot;wrap_content&quot;
  115. android:layout_marginStart=&quot;170dp&quot;
  116. android:layout_marginLeft=&quot;170dp&quot;
  117. android:layout_marginTop=&quot;400dp&quot;
  118. android:shadowRadius=&quot;3&quot;
  119. android:textColor=&quot;#0000FF&quot;
  120. android:text=&quot;Location sharing&quot;
  121. tools:ignore=&quot;HardcodedText&quot; /&gt;
  122. &lt;Button
  123. android:id=&quot;@+id/custom_button&quot;
  124. android:layout_width=&quot;wrap_content&quot;
  125. android:layout_height=&quot;wrap_content&quot;
  126. android:layout_marginTop=&quot;150dp&quot;
  127. android:layout_marginLeft=&quot;150dp&quot;
  128. android:background=&quot;@drawable/custom_button&quot;
  129. android:layout_marginStart=&quot;150dp&quot; /&gt;
  130. &lt;Switch
  131. android:id=&quot;@+id/switch_enable_button&quot;
  132. android:layout_width=&quot;wrap_content&quot;
  133. android:layout_height=&quot;wrap_content&quot;
  134. android:checked=&quot;true&quot;
  135. android:layout_marginLeft=&quot;200dp&quot;
  136. android:layout_marginTop=&quot;350dp&quot;
  137. android:text=&quot;Panic Alert&quot;
  138. android:layout_marginStart=&quot;200dp&quot;
  139. tools:ignore=&quot;HardcodedText&quot;/&gt;
  140. &lt;TextView
  141. android:id=&quot;@+id/textView3&quot;
  142. android:layout_width=&quot;80dp&quot;
  143. android:layout_height=&quot;wrap_content&quot;
  144. android:layout_marginTop=&quot;225dp&quot;
  145. android:layout_marginLeft=&quot;210dp&quot;
  146. android:text=&quot;Panic Mode&quot;
  147. android:textColor=&quot;@color/colorPrimaryDark&quot;
  148. android:textSize=&quot;20sp&quot;
  149. tools:ignore=&quot;HardcodedText&quot;
  150. android:layout_marginStart=&quot;200dp&quot; /&gt;
  151. &lt;Button
  152. android:id=&quot;@+id/report&quot;
  153. android:layout_width=&quot;120dp&quot;
  154. android:layout_height=&quot;wrap_content&quot;
  155. android:layout_marginTop=&quot;480dp&quot;
  156. android:layout_marginLeft=&quot;170dp&quot;
  157. android:text=&quot;Report&quot;
  158. android:textColor=&quot;#FF0000&quot;
  159. tools:ignore=&quot;HardcodedText&quot;
  160. android:layout_marginStart=&quot;180dp&quot; /&gt;
  161. &lt;TextView
  162. android:id=&quot;@+id/textView4&quot;
  163. android:layout_width=&quot;wrap_content&quot;
  164. android:layout_height=&quot;wrap_content&quot;
  165. android:layout_marginTop=&quot;130dp&quot;
  166. android:layout_marginLeft=&quot;150dp&quot;
  167. tools:ignore=&quot;HardcodedText&quot;
  168. android:text=&quot;Create your panic alert button &quot;
  169. android:layout_marginStart=&quot;150dp&quot; /&gt;
  170. &lt;Button
  171. android:id=&quot;@+id/button5&quot;
  172. android:layout_width=&quot;wrap_content&quot;
  173. android:layout_height=&quot;wrap_content&quot;
  174. android:layout_marginTop=&quot;650dp&quot;
  175. android:layout_marginLeft=&quot;150dp&quot;
  176. tools:ignore=&quot;HardcodedText&quot;
  177. android:text=&quot;Feedback&quot;
  178. android:textColor=&quot;#FFA500&quot;
  179. android:layout_marginStart=&quot;150dp&quot; /&gt;
  180. &lt;Button
  181. android:id=&quot;@+id/button6&quot;
  182. android:layout_width=&quot;wrap_content&quot;
  183. android:layout_height=&quot;wrap_content&quot;
  184. android:layout_marginTop=&quot;650dp&quot;
  185. android:layout_marginLeft=&quot;280dp&quot;
  186. tools:ignore=&quot;HardcodedText&quot;
  187. android:text=&quot;Write to us&quot;
  188. android:textColor=&quot;#FFA500&quot;
  189. android:layout_marginStart=&quot;280dp&quot; /&gt;
  190. &lt;/FrameLayout&gt;

java file

  1. package com.example.googlemaps;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.CompoundButton;
  7. import android.widget.Switch;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import com.google.firebase.auth.FirebaseAuth;
  11. public class HomeActivity extends AppCompatActivity {
  12. Button btnLogout;
  13. Button customButton;
  14. Switch switchEnableButton;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_home);
  19. Button btn = findViewById(R.id.btn);
  20. btn.setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. launchactivity();
  24. }
  25. });
  26. btnLogout = findViewById(R.id.logout);
  27. customButton = findViewById(R.id.custom_button);
  28. switchEnableButton = findViewById(R.id.switch_enable_button);
  29. customButton.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. Toast.makeText(HomeActivity.this, &quot;Click&quot;, Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. switchEnableButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  38. if (isChecked) {
  39. customButton.setEnabled(true);
  40. } else {
  41. customButton.setEnabled(false);
  42. }
  43. }
  44. });
  45. btnLogout.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. FirebaseAuth.getInstance().signOut();
  49. Intent intToMain = new Intent(HomeActivity.this, LoginActivity.class);
  50. startActivity(intToMain);
  51. }
  52. });
  53. }
  54. public void launchactivity(){
  55. Intent intent = new Intent( HomeActivity.this, MapsActivity.class );
  56. startActivity( intent );
  57. }
  58. }

I getting errors in homepage of my new app of helpline,I tried to check all but still not running please check and comment accordingly
Please review

答案1

得分: 0

你忘记了在你的片段中添加android:name=&quot;&quot;属性。如果你想要动态添加片段,请使用ViewGroup(例如FrameLayout等)。

英文:

You forgot about android:name=&quot;&quot; attribute in your fragment. If you want to add your fragment dynamically, please use ViewGroup (FrameLayout, etc.)

答案2

得分: 0

你在定义 XML 片段的代码有错误。请阅读这个链接:https://developer.android.com/training/basics/fragments/creating。
希望这能帮到你。

英文:

Your code to define a fragment in xml is incorrectly. Pls read this: https://developer.android.com/training/basics/fragments/creating.
Hope this help.

huangapple
  • 本文由 发表于 2020年4月9日 01:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61106848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定