Android:使用edittext和seekbar错误地扩展类片段

时间:2017-03-21 10:13:27

标签: android xml android-fragments

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

但是我收到错误"

  

错误扩充类片段

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

  

03-21 08:33:11.174 2830-2830 / com.example.utente.fragmentconmutamenti   E / AndroidRuntime:致命异常:主要       处理: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)

1 个答案:

答案 0 :(得分:0)

检查两个片段中的导入

你应该选择

import android.support.v4.app.Fragment;

而不是

import android.app.Fragment;

并点击

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