我正在尝试使用Eclipse和ADT完成Android HelloWorld App。但是,当我实施第二个活动并按“发送”按钮时,应用程序总是崩溃。以下是相关文件(导入被截断):
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
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.myfirstapp.MainActivity$PlaceholderFragment" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
fragment_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.myfirstapp.DisplayMessageActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
activity_display_message.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myfirstapp.DisplayMessageActivity"
tools:ignore="MergeRootFrame" />
MainActivity.java
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
最后,LogCat文件:
03-20 22:25:46.427: E/AndroidRuntime(32293): FATAL EXCEPTION: main
03-20 22:25:46.427: E/AndroidRuntime(32293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4137cd30 #0 id=0x7f05003c}
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.os.Looper.loop(Looper.java:137)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-20 22:25:46.427: E/AndroidRuntime(32293): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 22:25:46.427: E/AndroidRuntime(32293): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 22:25:46.427: E/AndroidRuntime(32293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-20 22:25:46.427: E/AndroidRuntime(32293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-20 22:25:46.427: E/AndroidRuntime(32293): at dalvik.system.NativeStart.main(Native Method)
03-20 22:25:46.427: E/AndroidRuntime(32293): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4137cd30 #0 id=0x7f05003c}
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.Activity.performStart(Activity.java:4479)
03-20 22:25:46.427: E/AndroidRuntime(32293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1941)
03-20 22:25:46.427: E/AndroidRuntime(32293): ... 11 more
我知道问题在于DisplayMessageActivity.java中onCreate的条件,但我不知道如何修复它。我很感激任何帮助,理解为什么这个简单的应用程序 - 基本上是从Android站点复制粘贴 - 将无法运行。
答案 0 :(得分:5)
我有类似的问题。当我重新检查你正在学习的教程,即developer.android时,我发现在DisplayMessageActivity类的onCreate方法中,下面的代码行导致了中断,(我已经评论过了)
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(message)).commit();
}
请删除这些行。如果你仔细观察那里的教程,他们最后会提交onCreate的代码进行交叉检查,缺少这些行。 :)
答案 1 :(得分:4)
在displayMessageActivity中,您将内容视图设置为textView,但它应该是activity_display_message.xml,因为这是您定义容器的位置。这就是为什么片段管理器找不到任何容器来添加片段的原因。您可以将文本传递给片段,以便它可以处理将文本设置为textView。
尝试类似:
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(message)).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private static final String ARG_MESSAGE = "ARG_MESSAGE";
public static PlaceholderFragment newInstance(String message) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
final String message = getArguments().getString(ARG_MESSAGE);
final TextView tv = (TextView) rootView.findViewById( id of your textview );
tv.setText(message);
return rootView;
}
}
答案 2 :(得分:2)
根据这条线:
No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4137cd30 #0 id=0x7f05003c}
您永远不会声明您的R.id.container
项目。它是对的 - 你永远不会在XML布局中声明它。我不确定你要用它做什么,但你应该将它添加到你的XML布局中,或者只是删除它(它看起来并不像你正在使用它)。
它看起来并不像您声明的片段。因为它只是一个简单的活动,所以不应该真正需要片段。