2012-08-06 190 views
2

當我的應用程序運行一個空指針異常時,我必須強制關閉。我認爲 我無法以正確的形式管理數據庫。強制在Android中關閉

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; 
    Intent in; 
    signup obj=new signup(); 

    /** 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); 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     //log in 
     if (arg0.getId() == R.id.bt1) 
     { 
      String name=ed1.getText().toString(); 
      int pass = Integer.parseInt(ed2.getText().toString()); 
      if (obj.c.getString(0) == name&&obj.c.getInt(1) == pass) 
      { 
       in = new Intent(this,secondview.class); 
       startActivity(in); 
      } 
      else 
      { 
       Toast.makeText(this, "please sing up first", 1000).show(); 
      } 
     } 
     else 
      if (arg0.getId()==R.id.bt2) 
      { 
       in=new Intent(this,signup.class); 
       startActivity(in); 
      } 
    } 
} 

其用於註冊新用戶,並把他們的信息在我的數據庫中的第二個活動:

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

public class signup extends Activity { 
    SQLiteDatabase sql; 
    Cursor c; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.singupx); 
     Intent in = getIntent(); 
     Bundle b = in.getExtras(); 
     String n = b.getString("x"); 
     int p = Integer.valueOf(b.getString("y")); 

     sql = openOrCreateDatabase("db",0, null); 
     sql.execSQL("CREATE TABLE if not exists employee " + 
        " (name text NOT NULL UNIQUE" + 
        ",password integer NOT NULL UNIQUE)"); 
     sql.execSQL("insert into employee(name) values(" + 
        "'"+n+"') "); 
     Toast.makeText(this,"Data inserted", 1000).show(); 
    } 
} 
+0

發佈您的logcat錯誤 – 2012-08-06 02:38:28

+2

Logcat錯誤報告將更有幫助複製並粘貼logcat給我們! – 2012-08-06 01:33:27

+0

不幸的是我會改變洞代碼..我會再次需要你的幫助:)非常感謝您的時間 – Syamic 2012-08-11 02:50:37

回答

3

這......很簡單。在你的第一個活動中,你沒有把"x""y"納入意圖。而在第二項活動中,你試圖得到它們。

... 
Bundle b=in.getExtras(); // perhaps this is null 
String n=b.getString("x"); // or this one 
int p=Integer.valueOf(b.getString("y")); // or this one 
+0

非常感謝4你其實這是因爲我改變了我的代碼幾次:) – Syamic 2012-08-11 02:48:51