开发者

Android TextView.setText force closes

I'm attempting Google University Android lab1 you are asked to change a TextView's text content according to the value passed via the Intent from another activity.

I tried out the rest of my code but... why does my app force close when I add the "tv.settext(...) line"?

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
 开发者_开发知识库   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
         * Fetch and display passed string.
         */
        TextView tv = (TextView) findViewById(R.id.HelloTV);
        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            String nameStr = extras.get("Username").toString();
            if (nameStr != null) {
                tv.setText("Hello "+nameStr);         
            }
        }
        setContentView(R.layout.main);
    }
}


Looking at the error log, and even better, looking at a debug session - it can be seen that there is a null pointer exception on line 22:

           tv.setText("Hello "+nameStr);         

This is because tv == null. It should have been initialised by the line:

    TextView tv = (TextView) findViewById(R.id.HelloTV);

but to use the id in the layout you must always register the view in the current activity. This line should have been included early in the onCreate method:

    setContentView(R.layout.main);

Here is the working Helloworld class:

public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*
         * Fetch and display passed string.
         */
        TextView tv = (TextView) findViewById(R.id.HelloTV);
        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            String nameStr;
            if (extras.get("Username") != null) {
                nameStr = extras.get("Username").toString();
                tv.setText("Hello "+nameStr);           
            }
        }
    }
}

This Helloworld class correctly retrieves the user's name from the extras sent when the activity starts and displays a personalised greeting.

I found the answer thanks to Konstantin Burov and the previous question here


Do you have only one .xml file in the project Layout folder? if you have an xml file for main activity and one fragment.xml file as it is offered by updated eclipse, you are required to do the setText inside the auto generated "onCreateView" function. This is where the fragment (which contains the view elements) and main layout are combined. so find the line below in your code or create it:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {  ...   }

and then set the text or other required elements of your view (such as btn, textView ...) inside this function. such as:

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

                    //set the text of textView
        TextView txvMain = (TextView) rootView.findViewById(R.id.txvMain);
        txvMain.setText("SetText works now");

                    //set a drawable as the background of the textView
        txvMain.setBackgroundResource(drawable.ic_launcher);

        return rootView;
    }

I will look forward for any further question. regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜