java.lang.IllegalStateException:片段com.Converter.myConverter.Unit.ArticleFragment没有创建视图

时间:2014-08-24 09:52:07

标签: android

我在类公共类中有一个例外ArticleFragment extends Fragment我得到了这个问题,但我不知道什么是错的,或者我得到了null spinner我不知道为什么或什么是争吵:

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



    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    getActivity().setContentView(R.layout.article_view);
    e=new ArrayList<String>();
    //dobime ime kategorije
 //   this.category = getIntent().getStringExtra("category");
    // dobimo enoto
    category="kot";
    this.enote = EnoteUpravljalec.getUnits(this.category, getActivity(), 1);
    String[] unitNames = new String[this.enote.size()];
    for (int i = 0; i < this.enote.size(); i++)
        unitNames[i] = enote.get(i).getLocalizedName();
    enoteAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, unitNames);
    enoteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);



    inputSpinner = (Spinner) getActivity().findViewById(R.id.unitInput1);
    outputSpinner = (Spinner) getActivity().findViewById(R.id.unitInput2);
try{
    inputSpinner.setAdapter(enoteAdapter);
    outputSpinner.setAdapter(enoteAdapter);
    inputSpinner.setOnItemSelectedListener(this);
    outputSpinner.setOnItemSelectedListener(this);



    EditText edit_text = (EditText)getActivity().findViewById(R.id.enotaVrednost);
    edit_text.addTextChangedListener(this);
    this.inputValid = false;

    setConversionOutput("");

    }catch(Exception ex){}

    this.unitInputIndex1 = -1;
    this.unitInputIndex2 = -1;
    this.inputRate1 = -1.0;
    this.inputRate2 = -1.0;
    // If activity recreated (such as from screen rotate), restore
    // the previous article selection set by onSaveInstanceState().
    // This is primarily necessary when in the two-pane layout.


    // Inflate the layout for this fragment
    return getView();
}

文章观点:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"


      >

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView">

            <LinearLayout 
                          android:orientation="vertical"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent"


                         >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="24dp"

       android:text="@+string/from_currency_amount_label"
        android:textSize="20sp"
        android:layout_gravity="left"/>

    <EditText
        android:id="@+id/currency_converter_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:textSize="25dp"
        android:padding="12dp"
        android:inputType="number|numberSigned|numberDecimal" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

     android:text="@+string/from_currency_label"
     android:textSize="20sp"
     android:layout_gravity="left"
     />
 <Spinner
     android:id="@+id/from_currency_spinner"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"

     android:paddingLeft="12dp"
     />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"

     android:text="@+string/to_currency_label"
     android:textSize="20sp"
     android:layout_gravity="left"
     />

 <Spinner
     android:id="@+id/to_currency_spinner"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"


     android:paddingLeft="12dp"
     />

 <TextView
     android:id="@+id/currency_conversion_output"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"

     android:text=""
     android:textSize="25sp"
     android:padding="12dp"
     android:paddingBottom="12dp"/>


                <TextView
                        android:id="@+id/last_updated"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"

                        android:text=""
                        android:textSize="18sp"
                        android:padding="12dp"
                        android:paddingBottom="12dp"/>

            </LinearLayout>

    </ScrollView>



</LinearLayout>

异常

08-24 11:48:05.621: E/AndroidRuntime(32489): FATAL EXCEPTION: main
08-24 11:48:05.621: E/AndroidRuntime(32489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.calculator.mycalculator/com.Converter.myConverter.Unit.EnotaMainHendler}: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.os.Looper.loop(Looper.java:137)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at java.lang.reflect.Method.invoke(Method.java:511)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at dalvik.system.NativeStart.main(Native Method)
08-24 11:48:05.621: E/AndroidRuntime(32489): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.Activity.setContentView(Activity.java:1881)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at com.Converter.myConverter.Unit.EnotaMainHendler.onCreate(EnotaMainHendler.java:21)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.Activity.performCreate(Activity.java:5104)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 11:48:05.621: E/AndroidRuntime(32489):    ... 11 more
08-24 11:48:05.621: E/AndroidRuntime(32489): Caused by: java.lang.IllegalStateException: Fragment com.Converter.myConverter.Unit.ArticleFragment did not create a view.
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:315)
08-24 11:48:05.621: E/AndroidRuntime(32489):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
08-24 11:48:05.621: E/AndroidRuntime(32489):    ... 21 more

2 个答案:

答案 0 :(得分:0)

在onCreateView中,您尝试返回getView()。这是不可能的,因为您尚未设置片段的视图。您可能需要做的事情如下:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.resturantmenu, container, false);
        // Do your view initialization here using v.findViewById()
        // Remember that you cannot reuse the views that are used in the activity already
        return v;
}

答案 1 :(得分:0)

对片段进行膨胀并在其中查找视图的正确方法如下:

public class HomeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        //To find a TextView (for example) from the inflated layout, you would do this:
        TextView text = (TextView) rootView.findViewById(R.id.myTextView);

        return rootView;
    }
}

此外,片段应始终独立于其活动,因为您永远不应从片段中的活动中访问内容。