gpt4 book ai didi

安卓 View InflateException : Binary XML file line #11 with runtime error

转载 作者:行者123 更新时间:2023-11-29 17:52:49 27 4
gpt4 key购买 nike

我想为再次提出这个主题而道歉,但我试图按照我发现的任何其他 Q/A 进行操作,但没有人为我工作。

另外,我刚开始为Android编程,所以请原谅我的无知。

由于我正在研究 Fragments,所以我试图在网络上找到一些可以清楚地解释这个主题的好例子。
这是我第一次练习时遵循的网络资源

Fragment Example

阅读并复制文件中的代码后,我注意更改包名称,但在运行应用程序后,这是我收到的 LogCat:

02-12 06:02:03.838: E/AndroidRuntime(1599): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.coppola.fragmentexample/it.coppola.fragmentexample.FragmentExampleActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.os.Looper.loop(Looper.java:136)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 06:02:03.838: E/AndroidRuntime(1599): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 06:02:03.838: E/AndroidRuntime(1599): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 06:02:03.838: E/AndroidRuntime(1599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 06:02:03.838: E/AndroidRuntime(1599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 06:02:03.838: E/AndroidRuntime(1599): at dalvik.system.NativeStart.main(Native Method)
02-12 06:02:03.838: E/AndroidRuntime(1599): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-12 06:02:03.838: E/AndroidRuntime(1599): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.Activity.setContentView(Activity.java:1929)
02-12 06:02:03.838: E/AndroidRuntime(1599): at it.coppola.fragmentexample.FragmentExampleActivity.onCreate(FragmentExampleActivity.java:13)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.Activity.performCreate(Activity.java:5231)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-12 06:02:03.838: E/AndroidRuntime(1599): Caused by: java.lang.NullPointerException
02-12 06:02:03.838: E/AndroidRuntime(1599): at it.coppola.fragmentexample.ToolbarFragment.onCreateView(ToolbarFragment.java:51)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-12 06:02:03.838: E/AndroidRuntime(1599): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

类和布局文件

FramentExampleActivity.java

package it.coppola.fragmentexample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.view.Menu;

public class FragmentExampleActivity extends FragmentActivity implements ToolbarFragment.ToolbarListener {

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

public void onButtonClick(int fontsize, String text) {
TextFragment textFragment =
(TextFragment)
getSupportFragmentManager().findFragmentById(R.id.text_fragment);
textFragment.changeTextProperties(fontsize, text);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.activity_fragment_example, menu);
return true;
}

}

TextFragment.java

package it.coppola.fragmentexample;

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

public class TextFragment extends Fragment {

private static TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.text_fragment, container, false);
textview = (TextView) view.findViewById(R.id.textView1);

return view;
}

public void changeTextProperties(int fontsize, String text)
{
textview.setTextSize(fontsize);
textview.setText(text);
}

}

ToolbarFrament.java

package it.coppola.fragmentexample;

import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ToolbarFragment extends Fragment implements OnSeekBarChangeListener {

private static int seekvalue = 10;
private static EditText edittext;
ToolbarListener activityCallback;

public interface ToolbarListener {
public void onButtonClick(int position, String text);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCallback = (ToolbarListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ToolbarListener");
}
}


@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.toolbar_fragment, container, false);

edittext = (EditText) view.findViewById(R.id.editText1);
final SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar1);

seekbar.setOnSeekBarChangeListener(this);

final Button button = (Button) view.findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});

return view;
}

public void buttonClicked (View view) {
activityCallback.onButtonClick(seekvalue, edittext.getText().toString());

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
seekvalue = progress;
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}

activity_fragment_example.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"
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=".FragmentExampleActivity" >

<fragment
android:id="@+id/toolbar_fragment"
class="it.coppola.fragmentexample.ToolbarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/toolbar_fragment" />

<fragment
android:id="@+id/text_fragment"
class="it.coppola.fragmentexample.TextFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
tools:layout="@layout/text_fragment" />

</RelativeLayout>

text_fragment.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Fragment Two"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

toolbar_fragment.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"
xmlns:tools="http://schemas.android.com/tools">

<Button
android.id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Change Text" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text" >
<requestFocus />

</EditText>

<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp" />

</RelativeLayout>

还有几个:

我确保我加载了 android-support-v4.jar 库,并且我没有以任何方式修改 AndroidManifest.xml。

感谢您愿意提供的任何帮助,感谢您的耐心等待。

最佳答案

您的工具栏 fragment 布局 XML 中存在拼写错误:

android.id="@+id/button1"

更改为android:id:

android:id="@+id/button1"

这会导致 findViewById() 返回 null 并尝试在 null 上调用 setOnClickListener()导致第 51 行出现 NPE。

关于安卓 View InflateException : Binary XML file line #11 with runtime error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21726621/

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