按钮按下无法更改TextView的文本

时间:2013-04-01 14:09:23

标签: android textview android-button

我想要的是每次用户从textview(q)中的一个文本开始时,textview(nm)显示用户所在的数字。简而言之,每次变量的值发生变化时,我都希望将该值设置为TexView(nm)处的文本。但是,这里显示错误。

按钮不起作用。怎么了?

logcat的

04-01 19:54:55.901: E/AndroidRuntime(32078): FATAL EXCEPTION: main
04-01 19:54:55.901: E/AndroidRuntime(32078): java.lang.NullPointerException
04-01 19:54:55.901: E/AndroidRuntime(32078):    at com.dreamgoogle.gihf.Quotes$1.onClick(Quotes.java:43)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.view.View.performClick(View.java:3524)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.view.View$PerformClick.run(View.java:14194)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.os.Handler.handleCallback(Handler.java:605)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.os.Looper.loop(Looper.java:137)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at android.app.ActivityThread.main(ActivityThread.java:4476)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
04-01 19:54:55.901: E/AndroidRuntime(32078):    at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:0)

“i”是一个整数。使用String.valueOf(i),StringBuilder或替换String.format(“%d”,i));

答案 1 :(得分:0)

如果您的视图已正确初始化,则最可能的是您的字符串数组为null。改变这个:

String[] str;

用这个

String[] str = new String[]{"Hahahaha", "Woohoo!", "It's-a-me, Mario!", "Mamma mia!"};

答案 2 :(得分:0)

问题是你已经声明了变量。但是你忘了初始化它们

ImageButton next;
ImageButton previous;
ImageButton copytext;
TextView q;
TextView nm;

对于这些广告,你必须对它们进行整理

例如:previous=(ImageButton)findViewById(R.id.name_in_your_layout_xml);

或者是否是动态的

previous= new ImageButton(context);

我想你忘记这样做。这就是为什么它展示了NPE

编辑:如果您使用任何功能设置此项,请将其称为onCreare() 在您的情况下,您必须在点击事件之前调用它

使用新代码编辑 你忘了给这个

nm = (TextView) findViewById(R.id.your_id_layout_xml);

答案 3 :(得分:0)

问题是nm从未绑定到视图中的组件。 这就是NPE的原因:

nm.setText(String.valueOf(i)) ;

将此添加到您的班级:

private void varSet() {
        next = (ImageButton) findViewById(R.id.next);
        previous = (ImageButton) findViewById(R.id.previous);
        copytext = (ImageButton) findViewById(R.id.copy);
        q = (TextView) findViewById(R.id.quotes);
        nm = (TextView) findViewById(R.id.YOUR_ID); // BOUND nm HERE WITH YOUR ID
        str = new String[] {
                "In order to succeed, your desire for success should be greater than your fear of failure.",
                "A successful man is one who can lay a firm foundation with the bricks others have thrown at him.",
                "Success is not final, failure is not fatal: it is the courage to continue that counts.",
                "Try not to become a man of success. Rather become a man of value.",
                 };

    };