2017-03-21 37 views
0

我正在嘗試編寫一個項目,其中包含帶有編輯文本,seekbar和按鈕的片段以及帶有textview的片段下方。如果您在editText上編寫代碼並移動搜索欄,則可以更改下面片段的textView中文本的內容和大小。我以前已經做了評價項目下面,然後我重新命名了一些要素和我糾正一些地方:hereAndroid:使用edittext和seekbar擴充類片段時出錯

不過,我得到一個錯誤「

Error inflating class fragment

」:下面是錯誤消息的一部分:

03-21 08:33:11.174 2830-2830/com.example.utente.fragmentconmutamenti E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.utente.fragmentconmutamenti, PID: 2830 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.utente.fragmentconmutamenti/com.example.utente.fragmentconmutamenti.MainActivity}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment

我讀過類似這樣的問題和他們的答案,但我仍然沒有找到我的代碼中有什麼錯誤。

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.Fragmentandroid.support.v4.app.Fragment;和getFragmentManager()與

getSupportFragmentManager()

,但它得到一個非常類似的錯誤:

Process: 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: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)

回答

0

檢查進口兩個片段的

你應該去

import android.support.v4.app.Fragment; 

而不是

import android.app.Fragment; 

和點擊

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

非常感謝你,我也做抄寫錯誤和線程上粘貼「TextFragment.java」的代碼,obiouvsly有開頭的進口: – Miche85

+0

如果你發現它是正確的,你可以將其標記爲真正的答案。 – 9spl

+0

正如我上面寫的,我用support.v4.app更正了代碼。片段和getSupportFragmentManager(),但我仍然有一個非常類似的錯誤與膨脹類。你對它的原因有什麼想法嗎? – Miche85