2012-05-28 55 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()上的空值拋出一個空指針。這是你的崩潰的原因嗎?

相關問題