简体   繁体   English

如何正确使用带有片段的活动?

[英]How to properly use an Activity with a Fragment within?

I'm trying to get an Activity which will work with a Fragment (I will work with more fragments later). 我试图获得一个可以与Fragment一起使用的Activity (稍后我将与更多片段一起使用)。 I created both a class and a layout for such Fragment , and included them at Activity 's layout. 我为此类Fragment创建了一个类和一个布局,并将它们包括在Activity的布局中。

Classes

This is what my Activity class looks like: 这是我的Activity类的样子:

public class Expressa extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is what my Fragment class looks like: 这是我的Fragment类的样子:

public class MessageFragment extends Fragment {

    EditText messageField;
    private onNewMessageListener  listener;

    public View OnCreateVie(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.message_fragment_layout,container,false);

         messageField = (EditText) view.findViewById(R.id.messageField);

        return view;
    }

    public void doTextToSpeech(View view) {

        String text = messageField.getText().toString().trim();
        new TextToSpeechServices().execute(text);
    }

    public interface onNewMessageListener {

        public void newMessage(String msg);

    }

}

Layouts 布局

Activity layout: Activity布局:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/messageFragment"
        class="apps.android.expressa.expressa.MessageFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/message_fragment_layout" />
</LinearLayout>

Fragment's: 片段的:

However I'm always getting an IllegalStateException complaining MessageFragment didn't create a view. 但是我总是收到一个IllegalStateException抱怨MessageFragment没有创建视图。

This is fragment's layout and this is complete stacktrace . 这是片段的布局 ,这是完整的stacktrace I'm using compile SDK API 22, build tools version 21.1.2 and min SDK is 16. What have I got wrong? 我正在使用编译SDK API 22,构建工具版本21.1.2,最小SDK是16。我怎么了?

The IllegalStateException is thrown because Android does not detect an implementation of onCreateView() in the MessageFragment . IllegalStateException是因为Android在MessageFragment中未检测到onCreateView()的实现。 You have spelled the name of the onCreateView() method incorrectly. 您拼写了onCreateView()方法的名称不正确。

Replace 更换

public View OnCreateVie(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

with

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

With the correct method signature and the @Override annotation, the framework will correctly interpret that you have specified a View for the Fragment . 使用正确的方法签名和@Override批注,框架将正确解释您已为Fragment指定一个View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM