06-02 23:08:18.863: W/dalvikvm(4388): threadid=1: thread exiting with uncaught exception (group=0x40ab7228)
06-02 23:08:18.883: E/AndroidRuntime(4388): FATAL EXCEPTION: main
06-02 23:08:18.883: E/AndroidRuntime(4388): 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{40df5418 #0 id=0x7f05003c}
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread.access$600(ActivityThread.java:139)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.os.Looper.loop(Looper.java:156)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread.main(ActivityThread.java:4987)
06-02 23:08:18.883: E/AndroidRuntime(4388): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 23:08:18.883: E/AndroidRuntime(4388): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 23:08:18.883: E/AndroidRuntime(4388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 23:08:18.883: E/AndroidRuntime(4388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 23:08:18.883: E/AndroidRuntime(4388): at dalvik.system.NativeStart.main(Native Method)
06-02 23:08:18.883: E/AndroidRuntime(4388): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{40df5418 #0 id=0x7f05003c}
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1195)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.Activity.performStart(Activity.java:4548)
06-02 23:08:18.883: E/AndroidRuntime(4388): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
06-02 23:08:18.883: E/AndroidRuntime(4388): ... 11 more
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity
{
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@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 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);
}
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;
}
}
public void sendMessage(View view)
{ // Called when user clicks the send button
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);
}
}
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
@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;
}
}
}
<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: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"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
<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.MainActivity"
tools:ignore="MergeRootFrame" />
答案 0 :(得分:2)
引起:java.lang.IllegalArgumentException:找不到id的视图 片段的0x7f05003c(com.example.myfirstapp:id / container) PlaceholderFragment {40df5418#0 id = 0x7f05003c}
看起来活动布局没有带有id容器的ViewGroup
。通常它是一个FrameLayout
,它充当你添加/替换片段的容器。
此活动也没有setContentView
两次。
答案 1 :(得分:1)
检查fragment_display_message.xml以获取警告。 在我的情况下有额外的&#34; /&gt;&#34;出现导致系统无法查找视图的问题。 一旦我删除并再次运行,app就会启动。