【问题标题】:Android: Error inflating class fragment, using edittext and seekbarAndroid:错误膨胀类片段,使用edittext和seekbar
【发布时间】:2017-03-21 20:06:14
【问题描述】:

我正在尝试编写一个带有活动的项目,其中有一个带有编辑文本、一个搜索栏和一个按钮的片段,以及一个带有文本视图的片段下方。如果您在 editText 上书写并移动搜索栏,则会更改下面片段的 textView 中文本的内容和大小。我使用了以下已经完成的评论项目,然后我重命名了一些元素并更正了一些部分:here

但是我得到一个错误“

膨胀类片段时出错

":以下是错误信息的一部分:

03-21 08:33:11.174 2830-2830/com.example.utente.fragmentconmutamenti E/AndroidRuntime: 致命异常: main 进程:com.example.utente.fragmentconmutamenti,PID:2830 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.utente.fragmentconmutamenti/com.example.utente.fragmentconmutamenti.MainActivity}: android.view.InflateException:二进制 XML 文件第 13 行:二进制 XML 文件第 13 行:膨胀类片段时出错

我阅读了类似的问题及其答案,但我仍然没有找到我的代码中的错误。

MainActivity.java

package com.example.utente.fragmentconmutamenti;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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



public void onButtonClick(int fontsize, String text) {

        TextFragment textFragment =
                (TextFragment)
                        getFragmentManager().findFragmentById(R.id.text_fragment);

        textFragment.changeTextProperties(fontsize, text);

}

}

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:id="@+id/activity_main"
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.utente.fragmentconmutamenti.MainActivity">

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.utente.fragmentconmutamenti.ToolbarFragment"
    android:id="@+id/toolbar_fragment"
    tools:layout="@layout/toolbar_fragment"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.utente.fragmentconmutamenti.TextFragment"
    android:id="@+id/text_fragment"
    android:layout_marginTop="130dp"
    android:layout_below="@+id/toolbar_fragment"
    android:layout_centerHorizontal="true"
    tools:layout="@layout/text_fragment" />
</RelativeLayout>

ToolbarFragment.java

package com.example.utente.fragmentconmutamenti;

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

public class ToolbarFragment

implements SeekBar.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");
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

    edittext = (EditText) view_toolbar_fragment.findViewById(R.id.editText1);

    final SeekBar seekbar =
            (SeekBar) view_toolbar_fragment.findViewById(R.id.seekBar1);

    seekbar.setOnSeekBarChangeListener(this);

    final Button button =
            (Button) view_toolbar_fragment.findViewById(R.id.button_text);


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

    return view_toolbar_fragment;

}

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) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

}


}

toolbar_fragment.xml

package com.example.utente.fragmentconmutamenti;

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

public class ToolbarFragment

implements SeekBar.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");
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

    edittext = (EditText) view_toolbar_fragment.findViewById(R.id.editText1);

    final SeekBar seekbar =
            (SeekBar) view_toolbar_fragment.findViewById(R.id.seekBar1);

    seekbar.setOnSeekBarChangeListener(this);

    final Button button =
            (Button) view_toolbar_fragment.findViewById(R.id.button_text);


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

    return view_toolbar_fragment;

}

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) {
}

@Override
public void onStopTrackingTouch(SeekBar arg0) {

}


}

TextFragment.java

 package com.example.utente.fragmentconmutamenti;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    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;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view_text_fragment =inflater.inflate(R.layout.text_fragment, container, false);

        textview = (TextView) view_text_fragment.findViewById(R.id.TextView1);

        return view_text_fragment;

    }

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

    }

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>

更新:我将android.app.Fragment 替换为android.support.v4.app.Fragment;和 getFragmentManager() 与

getSupportFragmentManager()

但它得到一个非常相似的错误:

进程:com.example.utente.fragmentconmutamenti,PID:3170

java.lang.RuntimeException:     Unable to start activity ComponentInfo   

{com.example.utente.fragmentconmutamenti/com.example.utente.fragmentconmutamenti.MainActivity}: android.view.InflateException:二进制 XML 文件第 13 行:二进制 XML 文件第 13 行:膨胀类片段时出错 在
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

【问题讨论】:

    标签: android xml android-fragments


    【解决方案1】:

    在两个片段中检查您的导入

    你应该去

    import android.support.v4.app.Fragment;
    

    而不是

    import android.app.Fragment;
    

    然后点击

    TextFragment textFragment =
                    (TextFragment)
                            getSupportFragmentManager().findFragmentById(R.id.text_fragment);
    

    【讨论】:

    • 非常感谢,我在线程上复制并粘贴“TextFragment.java”的代码时出错,但开头有导入:
    • 正如我上面写的,我用 support.v4.app.Fragment 和 getSupportFragmentManager() 更正了代码,但我仍然有一个非常相似的膨胀类错误。你知道它的原因吗?
    • 嗨,是的,如果您在同一活动中有两个框架,您应该尝试使用 FrameLayout Approch..检查此链接stackoverflow.com/questions/17580593/…它将帮助您理解..
    • 我阅读了链接,我明白我必须在activity_main.xml中将每个“
    • 您还需要在 FragmentTransaction 的帮助下添加或替换片段,您可以在同一链接中找到它并且不要忘记提交它。并删除 findFragmentById 它不会帮助你..
    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多