我开始学习Android语言,我正在尝试做这个教程: http://developer.android.com/training/basics/firstapp/starting-activity.html
但是我在“显示消息”部分遇到了问题。
如果我添加这部分代码,当我按下发送按钮时,应用程序强制关闭。
logcat(我不知道怎么发布:/):
06-05 16:12:03.213: E/FragmentManager(11276): No view found for id 0x7f080000 (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
06-05 16:12:03.213: E/FragmentManager(11276): Activity state:
06-05 16:12:03.221: E/FragmentManager(11276): Local Activity 41cc61d0 State:
06-05 16:12:03.221: E/FragmentManager(11276): mResumed=false mStopped=false mFinished=false
06-05 16:12:03.221: E/FragmentManager(11276): mLoadersStarted=false
06-05 16:12:03.221: E/FragmentManager(11276): mChangingConfigurations=false
06-05 16:12:03.221: E/FragmentManager(11276): mCurrentConfig={1.0 222mcc10mnc it_IT ldltr sw360dp w360dp h576dp 320dpi nrml long port finger -keyb/v/h -nav/h s.30 themeResource=null}
06-05 16:12:03.221: E/FragmentManager(11276): Active Fragments in 41cc62c8:
06-05 16:12:03.229: E/FragmentManager(11276): #0: PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
06-05 16:12:03.229: E/FragmentManager(11276): mFragmentId=#7f080000 mContainerId=#7f080000 mTag=null
06-05 16:12:03.229: E/FragmentManager(11276): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
06-05 16:12:03.229: E/FragmentManager(11276): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
06-05 16:12:03.229: E/FragmentManager(11276): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
06-05 16:12:03.229: E/FragmentManager(11276): mRetainInstance=false mRetaining=false mUserVisibleHint=true
06-05 16:12:03.229: E/FragmentManager(11276): mFragmentManager=FragmentManager{41cc62c8 in DisplayMessageActivity{41cc61d0}}
06-05 16:12:03.229: E/FragmentManager(11276): mActivity=com.example.myfirstapp.DisplayMessageActivity@41cc61d0
06-05 16:12:03.229: E/FragmentManager(11276): Added Fragments:
06-05 16:12:03.229: E/FragmentManager(11276): #0: PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
06-05 16:12:03.229: E/FragmentManager(11276): FragmentManager misc state:
06-05 16:12:03.229: E/FragmentManager(11276): mActivity=
06-05 16:12:03.229: E/FragmentManager(11276): com.example.myfirstapp.DisplayMessageActivity@41cc61d0
06-05 16:12:03.229: E/FragmentManager(11276): mContainer=android.app.Activity$1@41cc6340
06-05 16:12:03.229: E/FragmentManager(11276): mCurState=2 mStateSaved=false mDestroyed=false
06-05 16:12:03.229: E/FragmentManager(11276): Looper (main, tid 1) {41c68838}
06-05 16:12:03.229: E/FragmentManager(11276): (Total messages: 0, idling=false, quitting=false)
06-05 16:12:03.236: E/AndroidRuntime(11276): FATAL EXCEPTION: main
06-05 16:12:03.236: E/AndroidRuntime(11276): Process: com.example.myfirstapp, PID: 11276
06-05 16:12:03.236: E/AndroidRuntime(11276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2277)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread.access$800(ActivityThread.java:145)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.os.Handler.dispatchMessage(Handler.java:102)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.os.Looper.loop(Looper.java:136)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread.main(ActivityThread.java:5154)
06-05 16:12:03.236: E/AndroidRuntime(11276): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 16:12:03.236: E/AndroidRuntime(11276): at java.lang.reflect.Method.invoke(Method.java:515)
06-05 16:12:03.236: E/AndroidRuntime(11276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-05 16:12:03.236: E/AndroidRuntime(11276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-05 16:12:03.236: E/AndroidRuntime(11276): at dalvik.system.NativeStart.main(Native Method)
06-05 16:12:03.236: E/AndroidRuntime(11276): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.Activity.performStart(Activity.java:5319)
06-05 16:12:03.236: E/AndroidRuntime(11276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
06-05 16:12:03.236: E/AndroidRuntime(11276): ... 11 more
这是我认为存在问题的代码部分:
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
事实如果我删除
setContentView(textView);
新活动已成功打开,显然没有字符串消息。
这是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" />
感谢所有人:)
答案 0 :(得分:0)
我在一个空白项目中尝试了你的代码,它运行得很完美:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
// Set the text view as the activity layout
setContentView(textView);
}
}
你忘了打电话给super.onCreate吗?
答案 1 :(得分:0)
java.lang.IllegalArgumentException: No view found for id 0x7f080000 (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{41cce9e0 #0 id=0x7f080000}
在执行此处发布的片段事务时,使用setContentView()
设置的内容视图没有片段容器视图:
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
可能container
实际上位于activity_main
布局中,但您稍后将TextView
替换为onCreate()
。
删除PlaceholderFragment
和片段事务,或将TextView
放在活动或片段布局中,而不是在代码中动态创建。