ANDROID:强制关闭问题

时间:2014-05-15 08:41:12

标签: java android sqlite

我无法看到我的程序无效的原因。每当我尝试运行它时,它只会强行关闭。这是我的节目:

我的主要活动

package com.example.teacher;

public class MainActivity extends ActionBarActivity {

private DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"MyDatabase",2);

TextView text;
EditText txtPassword;
EditText teachersId;
EditText teachersName;
Button btnAdd;
Button btnDelete;
Button btnSearch;
Button btnPresent;
Button btnAbsent;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.textView1);
    txtPassword = (EditText) findViewById(R.id.txtPassword);
    teachersId = (EditText) findViewById(R.id.txtTeachersID);
    teachersName = (EditText) findViewById(R.id.txtTeachersName);
    btnPresent = (Button) findViewById(R.id.btnPresent);
    btnAbsent = (Button) findViewById(R.id.btnAbsent);
    btnAdd = (Button) findViewById(R.id.btnAddTeacher);
    btnDelete = (Button) findViewById(R.id.btnDeleteTeacher);

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try
    {

        db.execSQL("create table Users(ID integer, name varchar(90), pass varchar(90), attendance varchar(30));");
        text.setText("Successful in creating table.");
    }
    catch(Exception e)
    {    
        text.setText("Error in creating table.");
        e.printStackTrace();
    }

    btnAdd.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {


            if("".equals(teachersId.getText().toString()) || "".equals(txtPassword.getText().toString()))
              {
                  Toast toast = Toast.makeText(MainActivity.this, 
                          "Something needs to be typed in.", Toast.LENGTH_LONG); 
                toast.show();
              }
              else
              {
                  long flag = 0;
                  int id = 1;
                  SQLiteDatabase db = dbHelper.getWritableDatabase();
                  Cursor cursor = db.query("Users", new String[]{"count(*) ID"}, null, null, null, null, null);
                while(cursor.moveToNext())
                {
                    int idFromDatabase = cursor.getInt(cursor.getColumnIndex("ID"));
                    if(idFromDatabase != 0)
                    {
                        id = 1 + idFromDatabase;
                    }
                }

                  ContentValues values = new ContentValues();
                  values.put("ID", Integer.parseInt(teachersId.getText().toString().trim()));
                  values.put("name", teachersName.getText().toString().trim());
                  values.put("pass", txtPassword.getText().toString().trim());
                  flag = db.insert("Users", null, values);
                  if(flag != -1)
                  {
                      Toast toast = Toast.makeText(MainActivity.this, "A new record has been added! "
                              , Toast.LENGTH_LONG); 
                    toast.show();
                    db.close();
                    return;
                  }
                  else
                  {
                      Toast toast = Toast.makeText(MainActivity.this, "Something went wrong please check.", 
                              Toast.LENGTH_LONG); 
                    toast.show();
                    db.close();
                    return;
                  }

        }
        }

    });



    btnSearch.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            if("".equals(teachersId.getText().toString()))
            {
                Toast toast = Toast.makeText(MainActivity.this, "What is the reference ID number?",
                        Toast.LENGTH_LONG); 
                toast.show();
                return;
            }
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query("Users", new String[]{"ID","name","pass"}, "ID=?", 
                    new String[]{teachersId.getText().toString()}, null, null, null);
            if (!cursor.moveToNext())
            {
                Toast toast = Toast.makeText(MainActivity.this, "The ID is not existing!",
                        Toast.LENGTH_LONG); 
                toast.show();
                return;
            }


                cursor.moveToPrevious();
                while(cursor.moveToNext())
                {
                    int ID = cursor.getInt(cursor.getColumnIndex("ID"));
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String password = cursor.getString(cursor.getColumnIndex("pass"));
                    teachersName.setText(name);
                    txtPassword.setText(password);
                }
        }

         });


    btnDelete.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

            int flag = 0;
               SQLiteDatabase db = dbHelper.getWritableDatabase();
              flag = db.delete("Users", "ID=?", new String[]{""+teachersId.getText().toString().trim()});
              if(flag != 0)
              {
                  Toast toast = Toast.makeText(MainActivity.this, "The record has been deleted! ",
                          Toast.LENGTH_LONG); 
                toast.show();
                db.close();
                return;
              }
              else
              {
                  Toast toast = Toast.makeText(MainActivity.this, "Failed deleting the record! ",
                          Toast.LENGTH_LONG); 
                toast.show();
                db.close();
                return;
              }
           } });

    //--------------------------------------------------

}
}
    //--------------------------------------------------

我的活动主页

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation='vertical'
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.teacher.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/text"
    android:layout_width="279dp"
    android:layout_height="wrap_content"
    android:text="@string/a" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/teachersID" />

    <EditText
        android:id="@+id/txtTeachersID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/teachersName" />

    <EditText
        android:id="@+id/txtTeachersName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" android:inputType=""/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password" />

    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnPresent"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:text="@string/present" />

    <Button
        android:id="@+id/btnAbsent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/absent" />

</LinearLayout>

<Button
    android:id="@+id/btnAddTeacher"
    android:layout_width="285dp"
    android:layout_height="wrap_content"
    android:text="@string/addTeacher" />

<Button
    android:id="@+id/btnDeleteTeacher"
    android:layout_width="284dp"
    android:layout_height="wrap_content"
    android:text="@string/asadasda" />

<Button
    android:id="@+id/btnCheckTeacher"
    android:layout_width="281dp"
    android:layout_height="wrap_content"
    android:text="@string/browseTeacher" />

</LinearLayout>

我的dbhelper

package com.example.teacher;



public class DatabaseHelper extends SQLiteOpenHelper 
{
public DatabaseHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

private static final int VERSION = 1;

public DatabaseHelper(Context context,String name)
{
    this(context,name,VERSION);
}

public DatabaseHelper(Context context,String name,int version)
{
    this(context, name,null,version);
}

@Override
public void onCreate(SQLiteDatabase db) 
{
    //do nothing
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    // do nothing
}



}

根据logcat,它是由null异常引起的,但我真的不知道它在哪里以及如何处理它。请帮助解决这个力量关闭问题

05-15 04:22:20.579: E/AndroidRuntime(1067): FATAL EXCEPTION: main
05-15 04:22:20.579: E/AndroidRuntime(1067): Process: com.example.teacher, PID: 1067
05-15 04:22:20.579: E/AndroidRuntime(1067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teacher/com.example.teacher.MainActivity}: java.lang.NullPointerException
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.os.Looper.loop(Looper.java:136)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at dalvik.system.NativeStart.main(Native Method)
05-15 04:22:20.579: E/AndroidRuntime(1067): Caused by: java.lang.NullPointerException
05-15 04:22:20.579: E/AndroidRuntime(1067):     at com.example.teacher.MainActivity.onCreate(MainActivity.java:112)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.Activity.performCreate(Activity.java:5231)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 04:22:20.579: E/AndroidRuntime(1067):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 04:22:20.579: E/AndroidRuntime(1067):     ... 11 more
05-15 04:22:22.909: I/Process(1067): Sending signal. PID: 1067 SIG: 9

1 个答案:

答案 0 :(得分:3)

您尚未初始化btnSearch

将此添加到onCreate

 btnSearch= (Button) findViewById(R.id.idForSearchButton);

我认为可能是R.id.btnCheckTeacher