2012-08-11 57 views
-1

如果第一個活動已經有一個acount,那麼第一個活動會取用戶的名字和密碼,第二個view類將會打開,否則用戶應該首先唱歌。Android force關閉解析整數和引用按鈕

我有兩個共同的問題:

1-的整數原因力解析由logcat的接近通知

2-第二按鈕化妝也強制關閉


package sarah.android; 
import android.R.integer; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class SelesMeter2Activity extends Activity 
implements OnClickListener{ 
EditText ed1; 
EditText ed2; 
Button b1; 
Button b2; 
SQLiteDatabase sql; 
Cursor c; 
Intent in; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ed1=(EditText) findViewById(R.id.ed1); 
    ed2=(EditText) findViewById(R.id.ed2); 
    b1= (Button) findViewById(R.id.bt1); 
    b2= (Button) findViewById(R.id.bt2); 
    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    sql=openOrCreateDatabase("db",0, null); 
    sql.execSQL("CREATE TABLE if not exists " + 
      "Employee2 (password integer NOT NULL PRIMARY KEY,name text   NOT NULL)"); 
} 
@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    //log in 
    if(arg0.getId()==R.id.bt1) 
    { 
     String name=ed1.getText().toString(); 
     Integer pass=Integer.parseInt 
       (ed2.getText().toString()); 
     if(c.getCount()!=0) 
     { 
    c=sql.rawQuery("select * from Employee", null); 
    while(c.moveToNext()) 
    { 
    if(name.equals("c.getString(1)")&&pass==c.getInt(0)) 
    { 
     in=new Intent(this,secondview.class); 
     startActivity(in); 
     break; 
    } 
    } 
     } 

    else 
    { 
     Toast.makeText(this,"please sign up first or enter " + 
       "correct data", 2000).show(); 
    } 

    } 
    else if(arg0.getId()==R.id.bt2) 
    { 
     //sign up 
    Intent in2=new Intent(this,signup.class); 

     startActivity(in); 

    } 
} 
} 

輸入用戶新信息的唱歌班: 包sarah.android;

​​
+3

你有什麼問題嗎? – Raptor 2012-08-11 03:25:57

+0

我想將edittext的值轉換成整數,不用強制關閉 – Syamic 2012-08-11 03:28:22

+0

我有編號formate日誌貓異常 – Syamic 2012-08-11 03:29:21

回答

3

您需要先檢查您正在轉換的文本是否爲數值;否則,parseInt(...)將失敗。

可以使用try/catch做到這一點:

String sp = e2.getText().toString(); 
try { 
    // Attempt to parse the number as an integer 
    int p=Integer.parseInt(sp); 
    // This insertion will *only* execute if the parseInt was successful 
    obj.sql.execSQL("insert into Employee2  (password,name)values('"+n+"',"+p+")"); 
} catch (NumberFormatException nfe) { 
    // parseInt failed, so tell the user it's not a number 
    Toast.makeText(this, "Sorry, " + sp + " is not a number. Please try again.", Toast.LENGTH_LONG).show(); 
} 
+0

您的時間埃裏克但我十分感謝仍然有同樣的問題: – Syamic 2012-08-11 04:04:00

+0

日誌貓錯誤:java.lang.NumberFormatException:無法解析「」作爲整數 – Syamic 2012-08-11 04:04:36

+0

我在第一個活動修改了上單擊代碼通過嘗試catch將其包圍如你所說這是修改的部分: 如果(arg0.getId()== R.id.bt1) \t \t { \t \t \t INT p = 0; \t \t \t String name = ed1.getText()。toString(); \t \t \t String sp = ed2.getText()。toString(); \t \t \t嘗試{ \t \t \t //試圖解析該數爲一個整數 \t \t \t P =的Integer.parseInt(SP); \t \t \t} \t \t \t趕上(NumberFormatException的NFE){ \t \t \t // parseInt函數失敗,所以告訴用戶它不是一個數量 \t \t \t Toast.makeText(這一點, 「對不起,」 + SP +「不是數字,請重試。「,Toast.LENGTH_LONG).show(); – Syamic 2012-08-11 04:08:55