在运行时隐藏按钮时TextView出错

时间:2016-05-10 18:58:04

标签: android

  

在这个程序中。我收到与TextView相关的错误。此代码与单击按钮时返回值“1”有关。但问题是当我点击按钮。生成错误。有人可以解决这个问题。

public class Calculator extends Activity implements View.OnClickListener {

TextView display;
String screen;
String total="";
double reminder=0.0;
double remindertwo=0.0;
int sign=-1;
int shifts=1;
int dot=0;

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

    Button bone=(Button)findViewById(R.id.buttonone);
    bone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            total=total+1;
            display.setText("" + total);
        }
    });

    Button btwo=(Button)findViewById(R.id.buttontwo);
    btwo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            total=total+2;
            display.setText(""+total);
        }
    });
    Button bthree=(Button)findViewById(R.id.buttonthree);
    bthree.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            total=total+3;
            display.setText(""+total);
        }
    });
  

logcat的

05-11 00:29:00.443 83-85/? D/dalvikvm: GC_CONCURRENT freed 589K, 11% free 12275K/13767K, paused 13ms+13ms
05-11 00:29:11.262 868-868/? D/AndroidRuntime: CheckJNI is ON
05-11 00:29:12.902 868-868/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
05-11 00:29:12.932 83-236/? I/ActivityManager: Force stopping package scientific.calculator.cal.scientific_calculator uid=10047
05-11 00:29:12.992 868-868/? D/AndroidRuntime: Shutting down VM
05-11 00:29:13.032 868-879/? I/AndroidRuntime: NOTE: attach of thread 'Binder Thread #4' failed
05-11 00:29:13.093 868-872/? D/dalvikvm: GC_CONCURRENT freed 95K, 79% free 446K/2048K, paused 2ms+1ms
05-11 00:29:13.722 882-882/? D/AndroidRuntime: CheckJNI is ON
05-11 00:29:14.693 882-882/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
05-11 00:29:14.732 83-83/? I/ActivityManager: START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=scientific.calculator.cal.scientific_calculator/.Calculator} from pid 882
05-11 00:29:14.742 83-83/? W/WindowManager: Failure taking screenshot for (180x300) to layer 21005
05-11 00:29:14.882 83-111/? I/WindowManager: createSurface Window{415297e0 Starting scientific.calculator.cal.scientific_calculator paused=false}: DRAW NOW PENDING
05-11 00:29:14.882 882-882/? D/AndroidRuntime: Shutting down VM
05-11 00:29:14.972 882-893/? I/AndroidRuntime: NOTE: attach of thread 'Binder Thread #3' failed
05-11 00:29:15.042 894-894/? D/dalvikvm: Not late-enabling CheckJNI (already on)
05-11 00:29:15.063 83-95/? I/ActivityManager: Start proc scientific.calculator.cal.scientific_calculator for activity scientific.calculator.cal.scientific_calculator/.Calculator: pid=894 uid=10047 gids={}
05-11 00:29:15.162 882-886/? D/dalvikvm: GC_CONCURRENT freed 98K, 77% free 477K/2048K, paused 1ms+10ms
05-11 00:29:15.262 83-122/? W/NetworkManagementSocketTagger: setKernelCountSet(10047, 1) failed with errno -2
05-11 00:29:16.373 83-235/? I/WindowManager: createSurface Window{415532b0 scientific.calculator.cal.scientific_calculator/scientific.calculator.cal.scientific_calculator.Calculator paused=false}: DRAW NOW PENDING
05-11 00:29:16.462 894-894/? D/gralloc_goldfish: Emulator without GPU emulation detected.
05-11 00:29:16.572 83-111/? I/ActivityManager: Displayed scientific.calculator.cal.scientific_calculator/.Calculator: +1s729ms
05-11 00:29:16.852 83-122/? W/NetworkManagementSocketTagger: setKernelCountSet(10005, 0) failed with errno -2
05-11 00:29:17.922 894-894/? D/AndroidRuntime: Shutting down VM
05-11 00:29:17.922 894-894/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-11 00:29:17.942 894-894/? E/AndroidRuntime: FATAL EXCEPTION: main
05-11 00:29:17.942 894-894/? E/AndroidRuntime: java.lang.NullPointerException
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at scientific.calculator.cal.scientific_calculator.Calculator$1.onClick(Calculator.java:31)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.view.View.performClick(View.java:3480)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:13983)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:605)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 00:29:17.942 894-894/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
05-11 00:29:17.972 83-236/? W/ActivityManager:   Force finishing activity scientific.calculator.cal.scientific_calculator/.Calculator
05-11 00:29:17.982 83-236/? W/WindowManager: Failure taking screenshot for (180x300) to layer 21010
05-11 00:29:18.122 83-98/? I/WindowManager: createSurface Window{415cd570  paused=false}: DRAW NOW PENDING
05-11 00:29:18.492 83-97/? W/ActivityManager: Activity pause timeout for ActivityRecord{414193b8 scientific.calculator.cal.scientific_calculator/.Calculator}
05-11 00:29:18.512 83-122/? W/NetworkManagementSocketTagger: setKernelCountSet(10005, 1) failed with errno -2
05-11 00:29:18.582 83-235/? I/WindowManager: createSurface Window{416667c8 com.android.launcher/com.android.launcher2.Launcher paused=false}: DRAW NOW PENDING
05-11 00:29:19.512 83-122/? W/NetworkManagementSocketTagger: setKernelCountSet(10047, 0) failed with errno -2

1 个答案:

答案 0 :(得分:1)

您的显示为空,因为您从未设置它。在你的setContentView下面添加

display = (TextView)findViewById(R.id.YOURTEXTVIEWID);