gpt4 book ai didi

java - Fragments 中的 Android FATAL 异常

转载 作者:太空狗 更新时间:2023-10-29 13:13:25 27 4
gpt4 key购买 nike

我是 android 的新手。我收到以下错误。我研究了相同的问题并将答案应用到我的代码中。但我仍然遇到同样的错误。

05-28 09:30:12.800 2016-2016/com.example.yatisawhney.fragmentdemo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.yatisawhney.fragmentdemo, PID: 2016 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yatisawhney.fragmentdemo/com.example.yatisawhney.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714) at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) at com.example.yatisawhney.fragmentdemo.MainActivity.onCreate(MainActivity.java:13) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: Fragment com.example.yatisawhney.fragmentdemo.BottomFragment did not create a view. at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2319) at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357) at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690) at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) at com.example.yatisawhney.fragmentdemo.MainActivity.onCreate(MainActivity.java:13) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)


以下是我的java 3文件

MainActivity.java

package com.example.yatisawhney.fragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements TopFragment.ActivityCommunicator {

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

@Override
public void changeText(String a, String b) {
BottomFragment bf = (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
bf.setMeme(a,b);
}
}

底部 fragment

package com.example.yatisawhney.fragmentdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by yatisawhney on 28/05/16.
*/
public class BottomFragment extends Fragment {
private static TextView top ;
private static TextView bottom;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View bottom = inflater.inflate(R.layout.bottom_section ,container, false);

top = (TextView)bottom.findViewById(R.id.textView);
bottom=(TextView)bottom.findViewById(R.id.textView2);

return bottom;
}

public void setMeme(String a , String b){
top.setText(a);
bottom.setText(b);
}
}

顶部 fragment

package com.example.yatisawhney.fragmentdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
* Created by yatisawhney on 28/05/16.
*/
public class TopFragment extends Fragment{

private static EditText top;
private static EditText bottom;

ActivityCommunicator communicator;


public interface ActivityCommunicator{
public void changeText(String a , String b);
}

private void changeText(){
communicator.changeText(top.getText().toString() , bottom.getText().toString());
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communicator = (ActivityCommunicator) activity;
}catch(Exception e){
e.printStackTrace();
}
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View top_view = inflater.inflate(R.layout.top_section , container , false);

top=(EditText)top_view.findViewById(R.id.yates_text);
bottom=(EditText)top_view.findViewById(R.id.yates_another_text);
Button button = (Button)top_view.findViewById(R.id.yates_button);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
changeText();
}
}
);

return top_view;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.yatisawhney.fragmentdemo.MainActivity">

<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.yatisawhney.fragmentdemo.TopFragment"
android:id="@+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/top_section" />

<fragment
android:layout_width="300dp"
android:layout_height="300dp"
android:name="com.example.yatisawhney.fragmentdemo.BottomFragment"
android:id="@+id/fragment2"
tools:layout="@layout/bottom_section"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

最佳答案

我终于得到了问题的答案。

Fragment生命周期的onCreate可能会在Activity生命周期的onCreate中被调用。但并非总是如此

因此,为了安全起见,如果您希望访问 fragment 中的 View ,请在 onActivityCreated() 方法中进行。这让您确认 Activity 已创建并且 View 层次结构已加载到内存中。 :)

关于java - Fragments 中的 Android FATAL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499698/

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