0

我想要的是每次用户从文本视图(q)中的一个文本开始时,文本视图(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

4 回答 4

0

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

于 2013-04-01T14:16:39.337 回答
0

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

String[] str;

有了这个

String[] str = new String[]{"Hahahaha", "Woohoo!", "It's-a-me, Mario!", "Mamma mia!"};
于 2013-04-01T14:38:02.090 回答
0

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

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

对于这些widegets,你必须整合它们

例如: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);
于 2013-04-01T14:42:40.813 回答
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.",
                 };

    };
于 2013-04-01T14:49:28.860 回答