2012-05-28 32 views
0

任何人都知道如何避免Android应用程序崩溃时,EditText没有任何价值?当EditText没有任何内容时,Android程序崩溃

编辑:对不起,我发布了这个与我的手机。这里你去:

import java.util.Random; 

import android.app.Activity; 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.EditText; 
import android.widget.TextView; 

public class RandomButtonActivity extends Activity implements SensorEventListener 
{ 
    public float x = 0, y = 0, z = 0;   
    public float a = 10, b = 10, c = 10; 
    public float k = 4; 
    public int j = 0, i = 0; 
    public int rand = 0; 
    public String j1; 
    public String i1; 


    TextView testo; 
    TextView ultimo; 

    EditText limiteMin; 
    EditText limiteMax; 

    Random generator = new Random(); 

    SensorManager sm; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     testo = (TextView) findViewById(R.id.textView1); 
     ultimo = (TextView) findViewById(R.id.textView4); 

     limiteMin = (EditText) findViewById(R.id.editText2); 
     limiteMax = (EditText) findViewById(R.id.EditText01); 

     sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
     sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 

     random(); 
    } 

    public void onSensorChanged(SensorEvent event) 
    { 
     x = event.values[0]; 
     y = event.values[1]; 
     z = event.values[2];   

     if(x>a+k || x<a-k || y>b+k || y<b-k || z>c+k || z<c-k) 
     { 
      rand = random(); 
     } 
     a = x; 
     b = y; 
     c = z; 
    } 

    public int random() 
    { 
     j = Integer.parseInt(limiteMin.getText().toString()); 
     i = Integer.parseInt(limiteMax.getText().toString()); 
     int v = 0; 

      if(i==0 && j==0) 
      { 
       v = generator.nextInt();      
      } 
      else if(j>=i && j!=0) 
      {    
       v = 0; 
      }   
      else 
      { 
       i = i+1; 
       do 
       { 
       v = generator.nextInt(i); 
       testo.setText("Random: "+v); 
       } 
       while(v<j); 
      } 
      testo.setText("Random: "+v); 
      ultimo.setText("Random precedente: "+rand); 

     return v; 
    } 

    public void onAccuracyChanged(Sensor sensor, int accuracy) 
    { 
    } 
} 

只有当2个EditTexts中的1个为空并且调用方法random()时,程序才会崩溃。

+1

请上传您的错误代码以获取更多信息 – Lucifer

+0

发布您的代码和logcat信息和xml布局文件(如果有的话)... – drulabs

+2

您应该认识到,Android应用程序不会简单地在'EditText'(注意它不是'EditBox')是空的。很明显,问题在于您的特定应用程序及其对EditText小部件的使用。因此,你的问题,就像现在这样,是无法回答的。 – Felix

回答

2

应用下面的代码,

EditText et= (EditText)findViewById(R.id.editText); 
    if(et.getText.toString.equals(null) || et.getText.toString.equals("")) 
    { 
     Toast.makeText(this,"please enter something in text box",Toast.LENGTH_LONG).show(); 
    } 
+1

提示:使用android.text.TextUtils.isEmpty(http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence))检查字符串是否为空或空。 – andyandy

+0

我知道关于android..but的isEmpty方法,但它的初始级别理解它为什么会崩溃......反正谢谢.. – Hulk

0

你应该使用== opertaor检查空值。

et.getText() == null 

调用toString()上的空值抛出一个空指针。这是你的崩溃的原因吗?

相关问题