gpt4 book ai didi

安卓.view.InflateException : Binary XML file line #7: Error inflating class fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:45 26 4
gpt4 key购买 nike

我知道像这样的类似问题已经得到解答,但我似乎已经按照教程的建议进行操作并从其他帖子中阅读,但我仍然收到 Inflator 错误。不确定我做错了什么。请检查代码。非常感谢。

编辑:我在一个 Activity 中有两个 fragment 。我在一个 fragment 中有一个按钮,在另一个 fragment 中有一个 TextView 。我试图了解 fragment 是如何工作的,所以我在阅读文档后自己制作了这段代码。当按钮在一个 fragment 中被触发时,它会向 Activity 中的另一个 fragment 发送一个值。我无法让 fragment 膨胀

MainActivity.java

        import com.transport.mbtalocpro.PredictedTimeFragment.ButtonClickListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity implements ButtonClickListener {

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


@Override
public void sendCounter(int count) {
TestFrag testF = (TestFrag) getSupportFragmentManager().findFragmentById(R.id.bottomHalf);
if(testF != null) testF.setCounter(String.valueOf(count));

}

}

PredictedTimeFragment.java

            import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class PredictedTimeFragment extends Fragment {


ButtonClickListener bListener;

public interface ButtonClickListener {
public void sendCounter(int count);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
try {
View view = inflater.inflate(R.layout.prediction_time, container, false);
Button button = (Button) view.findViewById(R.id.test);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
test(v);

}
});
} catch(InflateException e) {
e.printStackTrace();
System.out.println("Predicted Time Fragment");
}
return super.onCreateView(inflater, container, savedInstanceState);
}

public void onAttach(Activity activity) {
super.onAttach(activity);
try {
bListener = (ButtonClickListener) activity;
} catch (ClassCastException e) {
System.out.println("must implemenet Listener");
}
}


public void test(View view) {
bListener.sendCounter(23);
}

}

TestFrag.java

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

public class TestFrag extends Fragment {

@Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflator, container, savedInstanceState);
try {
View view = inflator.inflate(R.layout.test_frag, container, false);
} catch(InflateException e) {
e.printStackTrace();
System.out.println("Test Fragment");
}
return super.onCreateView(inflator, container, savedInstanceState);
}

public void setCounter(String textS) {
TextView text = (TextView) getView().findViewById(R.id.test1);
text.setText((String) textS);
}
}

activity_main.xml

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

<fragment
android:id="@+id/topHalf"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:name="com.transport.mbtalocpro.PredictedTimeFragment"/>

<fragment
android:id="@+id/bottomHalf"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:name ="com.transport.mbtalocpro.TestFrag"/>

</LinearLayout>

predicted_time.xml

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/test"
android:text="Test"
android:layout_width="fill_parent"
android:layout_height="30dp"
/>

</LinearLayout>

test_frag.xml

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/test1"
android:layout_width="fill_parent"
android:layout_height="40dp"/>

</LinearLayout>

我也在尝试支持 pre-honey comb 版本,我想我正在导入所有支持库。

最佳答案

删除您的导入语句:

import android.support.v4.app.FragmentActivity;

然后使用弹出的导入建议再次导入。我不知道为什么,但它总是对我有用。

import android.support.v4.app.FragmentActivity; 

关于安卓.view.InflateException : Binary XML file line #7: Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15722787/

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