我有两个片段类,我在google developer doc之后添加了正确的片段事务。每当我在第一个片段布局中添加按钮时,我的应用程序崩溃。片段页面无法在应用程序中打开。所以我尝试没有按钮,一切正常。
代码如下: 第一个片段布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#a6c"
android:padding="16dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_weight="0.7">
<TextView
android:text="Word"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#ffffff"
android:textSize="40dp"
android:layout_marginBottom="20dp"
android:gravity="center"/>
<!--main word load-->
<TextView android:id="@+id/word"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/next"
android:layout_gravity="center"
android:text="Next"
android:textSize="?android:textAppearanceLarge"
/>
</LinearLayout>
</LinearLayout> </LinearLayout>
这是片段类
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
public PlaceholderFragment() {
}
View v;
TextView tv1;
Button checkAnswer,next,prev;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_card_front, container, false);
Log.v("Case","Working inside the first");
tv1 = (TextView) v.findViewById(R.id.word);
tv1.setText(CardFrontFragment.word[wordC]);
tv1.setOnClickListener(this);
next = (Button)v.findViewById(R.id.next);
next.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.next:
//LOad NExt wOrD
Log.v("Case","insideCase");
Fragment fragment = new CardBackFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}}
和logCat
10-09 15:00:36.320 1850-1850/com.zerothtech.greapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.zerothtech.greapp, PID: 1850
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zerothtech.greapp/com.zerothtech.greapp.CardFrontFragment}: android.view.InflateException: Binary XML file line #43: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
完整的错误日志位于&gt; http://pastebin.com/fRjhcqHw
我正在使用适当的代码在框架布局中添加它们。我添加片段没有问题。当我在布局中添加一些按钮(一个按钮与三个按钮相同)时,片段的功能停止工作。
提前致谢。
答案 0 :(得分:3)
logcat的:
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1 at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:572) at android.widget.TextView.<init>(TextView.java:999) at android.widget.Button.<init>(Button.java:111)
布局:
android:textSize="?android:textAppearanceLarge"
android:textAppearanceLarge
无法解析为维度。
在布局中使用android:textApperance
属性,或使用textSize
维度值。
答案 1 :(得分:0)
xml中没有Button
,标识为prev
。对于它,findViewById返回null,并尝试通过它访问setOnClickListener
导致您的应用程序崩溃。
答案 2 :(得分:0)
让你的Fragment类非静态。我认为这将解决你的问题。