【问题标题】:Android application stops after setText on TextViewAndroid 应用程序在 TextView 上的 setText 后停止
【发布时间】:2013-06-19 16:57:48
【问题描述】:

我有一个 android 程序,它在单击按钮后发送到另一个活动。主要是,我想在新窗口中设置 textview 中的文本,使其与所选按钮相对应。例如,如果我单击按钮 Writers,下一个新活动应该有一个文本视图,上面会显示单词 Writers。一切正常,除非我尝试在类别的 TextView 图标上设置文本。我还尝试在第一个活动中调用此更改,在启动第二个活动之前,它不起作用。 我还提到,如果我用 setText 注释该行,程序就可以正常工作。

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

这是第二个活动的 OnCreate 方法:

    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();
}

【问题讨论】:

    标签: android textview settext


    【解决方案1】:

    你需要在categoryTV = (TextView) findViewById(R.id.categoryTV);之前调用setContentView(R.layout.activity_play_game);,否则你的TextViewnull

       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();
        }
    

    您的Views 存在于您的Layout 中,因此如果您在使用setContentView()inflater 膨胀Layout 之前尝试访问它们中的任何一个,它们将是null,从而导致@ 987654332@ 当您尝试在它们上调用方法时,例如setText()

    【讨论】:

    • 我同意,这会在调用 setText(); 时导致 NPE;
    • 有效!非常感谢您的及时回复!我对 android 很陌生,我已经为此苦苦挣扎了几个小时。
    • 我现在有另一个问题,来自所选按钮的文本(在我的例子中是字符串)不会出现在新活动中,它只会显示为 null。
    • 如果我只写 CATEGORY_MESSAGE (因为它不知道从哪里获取变量),它不会编译。我尝试编写完全相同的键(CATEGORY_MESSAGE 的值)“e.c.project.CATEGORY”,但它仍然不起作用。此外,当我尝试使用 String category = playGameButton.getText().toString(); 运行程序时它停止了,所以我将其修改为 intent.putExtra(CATEGORY_MESSAGE, category);,其中 category 是全局字符串。
    • 如果我删除静态,它会说“不能对非静态元素进行静态引用”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多