如何正确使用带有 Fragment 的 Activity?
How to properly use an Activity with a Fragment within?
我正在尝试获得一个 Activity
,它将与 Fragment
一起使用(稍后我将使用更多片段)。我为这样的 Fragment
创建了一个 class 和一个布局,并将它们包含在 Activity
的布局中。
类
这是我的 Activity
class 的样子:
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);
}
}
这是我的 Fragment
class 的样子:
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);
}
}
布局
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>
片段的:
但是我总是收到 IllegalStateException
抱怨 MessageFragment
没有创建视图。
这是fragment's layout and this is complete stacktrace。我使用的是编译 SDK API 22,构建工具版本 21.1.2,最小 SDK 是 16。
我做错了什么?
IllegalStateException
被抛出,因为 Android 没有检测到 MessageFragment
中 onCreateView()
的实现。您拼错了 onCreateView()
方法的名称。
替换
public View OnCreateVie(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
使用正确的方法签名和 @Override
注释,框架将正确解释您为 Fragment
.
指定了 View
我正在尝试获得一个 Activity
,它将与 Fragment
一起使用(稍后我将使用更多片段)。我为这样的 Fragment
创建了一个 class 和一个布局,并将它们包含在 Activity
的布局中。
类
这是我的 Activity
class 的样子:
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);
}
}
这是我的 Fragment
class 的样子:
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);
}
}
布局
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>
片段的:
但是我总是收到 IllegalStateException
抱怨 MessageFragment
没有创建视图。
这是fragment's layout and this is complete stacktrace。我使用的是编译 SDK API 22,构建工具版本 21.1.2,最小 SDK 是 16。 我做错了什么?
IllegalStateException
被抛出,因为 Android 没有检测到 MessageFragment
中 onCreateView()
的实现。您拼错了 onCreateView()
方法的名称。
替换
public View OnCreateVie(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
使用正确的方法签名和 @Override
注释,框架将正确解释您为 Fragment
.
View