的setText上的TextView后的And​​r​​oid应用程序将停止 [英] Android application stops after setText on TextView

查看:141
本文介绍了的setText上的TextView后的And​​r​​oid应用程序将停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点击一个按钮后,发送到另一个活动的Andr​​oid程序。主要是,我想从设置TextView的文本在新的窗口,以便它对应于所选择的按钮。举例来说,如果我按一下按钮作家,下一个新的活动应具备的词作家出现在其中一个TextView。一切正常,除非我尝试的setText对类TextView的图标。我也试着拨打这个变化中的第一项活动,推出第二个之前,它没有工作。 我还提到,如果我评论用的setText行,程序工作得很好。

 私有String类;
公共最后静态字符串CATEGORY_MESSAGE =ecproject.CATEGORY;
公共无效的onClick(视图v){
    开关(v.getId())
        {
            案例R.id.actors:
                类=演员;
                playTheGame(五);
            打破;
            案例R.id.cartoons:
                类=卡通;
                playTheGame(五);
        打破;
            案例R.id.singers:
                类=歌手;
                playTheGame(五);
            打破;
            案例R.id.writers:
                类=作家;
                playTheGame(五);
            打破;
        }
    }

    公共无效playTheGame(查看视图){
        意向意图=新的意图(这一点,PlayGame.class);
        字符串类= playGameButton.getText()的toString()。
        intent.putExtra(CATEGORY_MESSAGE,类);
// TextView的电视=(TextView中)findViewById(R.id.categoryTV);
// tv.setText(类);
        startActivity(意向);
    }
 

这是从第二个活动的onCreate方法:

 私人TextView的categoryTV;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    意向意图= getIntent();
    字符串类= intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

    categoryTV =(TextView中)findViewById(R.id.categoryTV);
    categoryTV.setText(类);

    的setContentView(R.layout.activity_play_game);
    //显示操作栏中的向上按钮。
    setupActionBar();
}
 

解决方案

您需要调用的setContentView(R.layout.activity_play_game); categoryTV =(TextView中)findViewById(R.id.categoryTV); 否则,你的的TextView

 私人TextView的,categoryTV;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
       的setContentView(R.layout.activity_play_game); //使这个调用初始化之前的任何意见
       意向意图= getIntent();
       字符串类= intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

       categoryTV =(TextView中)findViewById(R.id.categoryTV);
       categoryTV.setText(类);


        //显示操作栏中的向上按钮。
         setupActionBar();
    }
 

查看取值存在于你的布局因此,如果您尝试充气之前访问任何一个你的布局,与的setContentView()充气,他们将导致 NPE 当您试图调用他们的方法,如的setText( )

I have an android program which sends to another activity after clicking on a button. Mainly, I want to set the text from textview in the new windows so that it corresponds to the selected button. For example, if I click on button Writers, the next new activity should have a textview on which the word Writers appears. Everything works fine, except when I try to setText on the TextView icon for category. I also tried to call this change in the first activity, before launching the second one, it didn't work. I also mention that if I comment the line with the setText, the program works just fine.

private String          category;
public final static String CATEGORY_MESSAGE = "e.c.project.CATEGORY";
public void onClick(View v) {
    switch(v.getId())
        {
            case R.id.actors:
                category = "actors";
                playTheGame(v);
            break;
            case R.id.cartoons:
                category = "cartoons";
                playTheGame(v);
        break;
            case R.id.singers:
                category = "singers";
                playTheGame(v);
            break;
            case R.id.writers:
                category = "writers";
                playTheGame(v);
            break;
        }
    }

    public void playTheGame( View view ){
        Intent intent = new Intent(this, PlayGame.class);
        String category = playGameButton.getText().toString();
        intent.putExtra(CATEGORY_MESSAGE, category);
//      TextView tv = (TextView) findViewById(R.id.categoryTV);
//      tv.setText(category);
        startActivity(intent);
    }

this is the OnCreate method from the second activity:

    private TextView            categoryTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

    categoryTV = (TextView) findViewById(R.id.categoryTV);
    categoryTV.setText(category);

    setContentView(R.layout.activity_play_game);
    // Show the Up button in the action bar.
    setupActionBar();
}

解决方案

You need to call setContentView(R.layout.activity_play_game); before categoryTV = (TextView) findViewById(R.id.categoryTV); Otherwise your TextView is null

   private TextView, categoryTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_play_game);  // make this call BEFORE initializing ANY views
       Intent intent = getIntent();
       String category = intent.getStringExtra(GameCategories.CATEGORY_MESSAGE);

       categoryTV = (TextView) findViewById(R.id.categoryTV);
       categoryTV.setText(category);


        // Show the Up button in the action bar.
         setupActionBar();
    }

Your Views exist in your Layout so if you try to access any of them before inflating your Layout, with setContentView() or an inflater, they will be null resulting in a NPE when you try to call a method on them such as setText()

这篇关于的setText上的TextView后的And​​r​​oid应用程序将停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆