-1

如果他已经有帐户,则第一个活动将获取用户的名称和密码,否则将打开第二个视图类,否则用户应先唱歌。

我有两个常见问题:

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);

    }
}   
}

输入用户新信息的sing up类:package sarah.android;

    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.EditText;
    import android.widget.Toast;

   public class signup extends Activity
   implements OnClickListener{
EditText e1;
EditText e2;
SelesMeter2Activity obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singupx);
    Intent in =getIntent();
    e1=(EditText) findViewById(R.id.ed1s);
    e2=(EditText) findViewById(R.id.ed2s);
    obj=new SelesMeter2Activity();
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String n=e1.getText().toString();
    int p=Integer.parseInt(e2.getText().toString());
    obj.sql.execSQL("insert into Employee2        (password,name)values('"+n+"',"+p+")");

}

}
4

1 回答 1

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();
}
于 2012-08-11T03:36:40.513 回答