如何从按钮外部或onCreate写入TextView

时间:2014-07-09 05:39:03

标签: java android nullpointerexception

好的,还有另一个noobie问题的时间:

我已经在Activity类的顶部声明了一个TextView作为静态,我认为它应该使它成为" global"。然后在onCreate中我将该变量附加到TextView。看起来像这样:

public class CheckerActivity extends Activity {

String testNumber = "0";
public static TextView displayArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checker);

    int[] numbersArray;
    Intent intent = getIntent();
    numbersArray = intent.getIntArrayExtra(EnterCurrentNumbersActivity.CURRENT_NUMBERS_ARRAY);
    String arrayStringed = Arrays.toString(numbersArray);
    displayArray = (TextView) findViewById(R.id.currentNumbers);
    displayArray.setText(arrayStringed);
}

然后在一个方法中,我在同一个类中创建了自己,我尝试使用TextView声明并向其写入内容。像这样:

public void addNumber(String[] numbersToAdd) {

    this.setContentView(R.layout.activity_checker);

    displayArray.setText(Arrays.toString(numbersToAdd));
}

然后我有一个按钮调用addNumber方法,如下所示:

public void populateButton(View view) {

        String[] temp = {"test", "array"};
        CheckerActivity addNumbers = new CheckerActivity();
        addNumbers.addNumber(temp);
}

但是当单击该按钮时,我一直在addNumber方法上得到NullPointerException。

我已经搜遍了这一点,但我想我不知道该问什么谷歌。据我所知,这应该有用,哪里出错?

在你的sugestions之后

logcat。 (但是Eldar Mensutov得到了它。立即行动!)

07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.NullPointerException 07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.addNumber(CheckerActivity.java:85) 07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:37) 07-09 15:49:48.542 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 07-09 15:49:48.552 21036-21036/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

为了传达这个问题,我现在想打个电话     public void addNumber(String[] numbersToAdd) 来自完全不同的类(同一项目中的完整不同文件) 使用this.addNumber(temp);无效。 有什么建议??

1 个答案:

答案 0 :(得分:1)

不要打电话

this.setContentView(R.layout.activity_checker);

public void addNumber(String[] numbersToAdd)

并替换

   CheckerActivity addNumbers = new CheckerActivity();
   addNumbers.addNumber(temp);

   this.addNumber(temp);

让你的字段不是静态的:public TextView displayArray;