我应该用什么视图setContentView?

时间:2014-06-17 12:00:30

标签: android android-activity android-view

我只是尝试使用intent启动第二项活动,而使用第二项活动的onCreate方法我不确定我是不是将我的setContentView()设置为正确的视图...

的onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_display_information);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    EditText toTestView            = null, 
             fromTestView          = null, 
             subjectTestView       = null, 
             messageTestView       = null;

    String recievedToMessage       = " ",
            recievedFromMessage    = " ",
            recievedSubjectMessage = " ", 
            recievedMessage        = " " ;

    Intent i = getIntent();
    Bundle e = i.getExtras();

    if (e != null){


        recievedToMessage = e.getString(MainActivity.TO_MESSAGE);
        recievedFromMessage = e.getString(MainActivity.FROM_MESSAGE);
        recievedSubjectMessage = e.getString(MainActivity.SUBJECT_MESSAGE);
        recievedMessage = e.getString(MainActivity.MESSAGE);

        //setContentView(R.layout.activity_display_information);

        toTestView =  (EditText) findViewById(R.id.recievedToText);
        fromTestView = (EditText) findViewById(R.id.recievedFromText);
        subjectTestView = (EditText) findViewById(R.id.recievedSubjectText);
        messageTestView = (EditText) findViewById(R.id.recievedMessageText);

        toTestView.setText("To: " + recievedToMessage);
        fromTestView.setText("From: " + recievedFromMessage);
        subjectTestView.setText("Subject: " + recievedSubjectMessage);
        messageTestView.setText(recievedMessage);
    }

}

我有2秒的活动xmls,fragment_display_information.xmlactivity_display_information.xml截至目前,当我尝试访问应用的第二页时,我收到了Unortunately 'Appname' has stopped working.消息,并且在LogCat中我得到一个AndroidRuntime ErrorView could not be found

logcat的:

06-17 07:59:03.020: E/AndroidRuntime(1216): FATAL EXCEPTION: main
06-17 07:59:03.020: E/AndroidRuntime(1216): Process:  com.example.multipledatathroughactivitypractice, PID: 1216
06-17 07:59:03.020: E/AndroidRuntime(1216): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multipledatathroughactivitypractice/com.example.multipledatathroughactivitypractice.DisplayInformation}: java.lang.IllegalArgumentException: No view found for id 0x7f050040 (com.example.multipledatathroughactivitypractice:id/container) for fragment PlaceholderFragment{b2cf4030 #0 id=0x7f050040}
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.os.Looper.loop(Looper.java:136)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at java.lang.reflect.Method.invoke(Method.java:515)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at dalvik.system.NativeStart.main(Native Method)
06-17 07:59:03.020: E/AndroidRuntime(1216): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f050040 (com.example.multipledatathroughactivitypractice:id/container) for fragment PlaceholderFragment{b2cf4030 #0 id=0x7f050040}
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.Activity.performStart(Activity.java:5241)
06-17 07:59:03.020: E/AndroidRuntime(1216):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
06-17 07:59:03.020: E/AndroidRuntime(1216):     ... 11 more

2 个答案:

答案 0 :(得分:2)

您需要删除此代码

if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); }

并更改此

setContentView(R.layout.activity_display_information);

答案 1 :(得分:0)

似乎你在create上的活动中膨胀错误的布局。如下所示。

setContentView(R.layout.activity_display_information);