2012-12-17 102 views
0

您可以告诉同一个程序在一台机器上运行,并且不会在另一台机器上运行吗?它在另一个表示空指针异常。当我点击CheckBox时,它不幸地说你的活动已经停止。在一台机器上运行的Android程序没有在另一台机器上运行

下面是代码: -

package com.example.gtbactivity; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnCheckedChangeListener, OnClickListener { 

    CheckBox cb1,cb2; 
    TextView t1,t2,t3; 
    Button b; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     cb1=(CheckBox)findViewById(R.id.checkBox1); 
     cb2=(CheckBox)findViewById(R.id.checkBox2); 
     b=(Button)findViewById(R.id.button1); 
     t1=(TextView)findViewById(R.id.textView1); 
     t2=(TextView)findViewById(R.id.textView2); 
     t3=(TextView)findViewById(R.id.textView3); 


     b.setOnClickListener(this); 
       cb1.setOnCheckedChangeListener(this); 
       cb2.setOnCheckedChangeListener(this); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 



    @Override 
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
     if(cb1.isChecked()) 
     { t1.setText("10");} 
     else 
     {t1.setText("0");} 


     if(cb2.isChecked()) 
     { t2.setText("15");} 
     else 
     {t2.setText("0");} 


    } 
    @Override 
    public void onClick(View arg0) { 
     int total,a,b; 
     a=Integer.parseInt(t1.getText().toString()); 
     b=Integer.parseInt(t2.getText().toString()); 

     total=a+b; 
     t3.setText(String.valueOf(total)); 

    } 

} 

回答

2

我没有看到的TextView T1被初始化,这也许就是你得到空指针异常

而不是

t2=(TextView)findViewById(R.id.textView1); 

使用

的原因
t1=(TextView)findViewById(R.id.textView1); 
相关问题