Android 将代码从基于活动的UI传输到基于片段的UI

Android 将代码从基于活动的UI传输到基于片段的UI,android,android-fragmentactivity,Android,Android Fragmentactivity,由于在fragment.xml中创建的文本视图及其代码被添加到主活动类中,我在第39行遇到了一个空指针异常。我应该如何处理突出显示的代码段,使其以相同的方式工作,但在PlaceHolderFragment类中 package com.example.myfirstapp; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support

由于在fragment.xml中创建的文本视图及其代码被添加到主活动类中,我在第39行遇到了一个空指针异常。我应该如何处理突出显示的代码段,使其以相同的方式工作,但在PlaceHolderFragment类中

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.DatePicker;
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();
        } */

        getSupportActionBar().show();



        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        ***TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setTextSize(40);
        textView.setText(message);***

        // Set the text view as the activity layout
        setContentView(R.layout.fragment_display_message);






    }

    @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;
        }


    }

}

如果textview与activity添加到fragment layoutsame中,则它的膨胀应该在fragment类内部。因此,将textview移动到片段的onCreateView方法中。它应该是这样的:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.fragment_calendar, container,
            false);
    TextView textView = (TextView)view.findViewById(R.id.textView1);//make this a class variable
    textView.setTextSize(40);//put this method in onStart()
    textView.setText(message);//put this method in onStart()
    return view;
}

嗨,我想你应该写信

    // Create the text view
    TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setTextSize(40);
    textView.setText(message);
下面

        setContentView(R.layout.fragment_display_message);

引用片段元素的任何代码都应该在片段内部完成。活动不应该知道关于片段UI内部的任何信息。@Gabeschen我知道这个事实。然而,我是android新手,无法理解这段代码文本视图部分的相似性,它将由fragment类处理。不太清楚您希望我做什么。首先,TextView TextView=TextViewview.findViewByIdR.id.textView1;给出了一个错误。我尝试用getView替换view,结果成功了。下一个问题是它无法访问变量消息,因为它的引用已在activity类中绘制。