移动到另一个活动时我的android应用程序关闭

时间:2018-01-18 21:40:50

标签: android sqlite

我是新来的,也是一般的编程新手

我正在使用SQLite创建一个献血的Android应用程序,当我移动到另一个活动以显示它关闭的捐赠者时。

我使用DB Helper创建了一个数据库。这是代码:

public class BloodBankDatabaseHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "Bloodbank";
    private static final int DB_VERSION = 1;

    BloodBankDatabaseHelper (Context context){

        super(context, "Bloodbank", null, 1 );
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE DONOR ("
        + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + "NAME TEXT,"
        + "BLOOD TEXT, "
        + "PHONE TEXT, " + " DISTRICT TEXT"
        + " EMAIL TEXT);");

        insertDonor(db, "Bilal Achi", "O+", "03567773", "Beirut", "bilal.achi@hotmail.com");
        insertDonor(db, "Mohamad", "O-", "03024026","Beirut",  "Mohamad@hotmail.com");
        insertDonor(db, "Fadi", "A-", "03444333","Beirut", "Fadi211@hotmail.com");
        insertDonor(db, "Hadi", "B-", "03123122", "Beirut", "Hadi@hotmail.com");
        insertDonor(db, "Rana", "AB+", "03974267", "Beirut", "Rana121@hotmail.com");
        insertDonor(db, "Sara", "A+", "03123891", "Beirut", "Sarah@hotmail.com");
        insertDonor(db, "Khalil", "B-", "03456189", "Beirut", "Khalil088@hotmail.com");
        insertDonor(db, "Jad", "O-", "03999211", "Tyre", "Jad992@hotmail.com");
        insertDonor(db, "Siham", "AB+", "03213477", "Tyre", "Siham1112@hotmail.com");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + "DONOR");
        onCreate(db);
    }

    private static void insertDonor(SQLiteDatabase db, String name, String blood, String phone,
                                   String disctrict, String email) {

        ContentValues donorValues = new ContentValues();
        donorValues.put("NAME",name);
        donorValues.put("BLOOD", blood);
        donorValues.put("PHONE", phone);
        donorValues.put("Email", email);
        donorValues.put("DISTRICT",disctrict);
        db.insert("DONOR", null, donorValues );
    }
}

我的主要活动包括两个微调器来指定区域和血型以及一个移动到活动的按钮

以下是主要活动的代码

public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase db;


    private Spinner sp1;
    private Spinner sp2;
    private Button bt1;

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

        sp1 = (Spinner)findViewById(R.id.spnr1);
        sp2 = (Spinner)findViewById(R.id.spnr2);
        bt1 = (Button)findViewById(R.id.btnSearch);

        String[] Districts = {"Aley ","Akkar ","Baabda ","Baalback ","Batroun ","Beirut ",
                "Bsharri ","Chouf ","Hasbaya ","Hermel ","Jbeil ","Jizzine ","Keserwan ",
                "Koura ", "Marjeyoun ","Matn ","Miniyeh-Danniyeh ","Nabatieh ","Rashaya ","Sydon ",
                "Tripoli ","Tyre ","Western Bekaa ","Zahle ","Zgharta "};

        String [] BloodTypes = {"O+ ", "O- ","A+ ","A- ","B+ ",
        "B- ","AB+ ","AB- "};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, Districts);
        sp1.setAdapter(adapter);

        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, BloodTypes);
        sp2.setAdapter(adapter2);

        sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = ((TextView)view).getText().toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = ((TextView)view).getText().toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Districts = sp1.getSelectedItem().toString();
                String bloodtypes = sp2.getSelectedItem().toString();
                Intent intent = new Intent(MainActivity.this, Donor.class);
                startActivity(intent);
            }
        });

    }
}

现在按下按钮时,新活动应该开始显示捐赠者,但应用程序崩溃这是代码

public class Donor extends AppCompatActivity {

    public static final String Extra_Disctrict = "Districts";
    public static final String Extra_type = "bloodtypes";

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

    String Districts = (String)getIntent().getExtras().get(Extra_Disctrict);
    String bloodtypes = (String)getIntent().getExtras().get(Extra_type);


    //Create a Cursor

    try {
        SQLiteOpenHelper BloodBankDatabaseHelper = new BloodBankDatabaseHelper(this);
        SQLiteDatabase db = BloodBankDatabaseHelper.getReadableDatabase();
        Cursor cursor = db.query("Bloodbank",
                new String[]{"NAME", "PHONE", "EMAIL"},
                "DISTRICT = ? AND BLOOD = ?",
                new String []{(Districts),(bloodtypes)}, null, null, null);

        if (cursor.moveToFirst()) {

            String nameText = cursor.getString(0);
            String phoneText = cursor.getString(1);
            String emailText = cursor.getString(2);

            TextView name = (TextView)findViewById(R.id.name);
            name.setText(nameText);

            TextView phone = (TextView)findViewById(R.id.phone);
            phone.setText(phoneText);
            TextView email = (TextView)findViewById(R.id.email);
            email.setText(emailText);

        }
        cursor.close();
        db.close();

    } catch(SQLiteException e) {
        Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
        toast.show();
        }
    }
}

这里是Logcat

01-19 00:10:03.491 3316-3316/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.example.bilal.bloodbank
01-19 00:10:03.521 3316-3316/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.bilal.bloodbank, PID: 3316
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilal.bloodbank/com.example.bilal.bloodbank.Donor}: java.lang.NullPointerException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NullPointerException
                                                     at com.example.bilal.bloodbank.Donor.onCreate(Donor.java:24)
                                                     at android.app.Activity.performCreate(Activity.java:5397)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                     at dalvik.system.NativeStart.main(Native Method) 
01-19 00:10:04.171 4174-4174/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.example.bilal.bloodbank
01-19 00:10:04.201 4174-4174/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.bilal.bloodbank, PID: 4174
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilal.bloodbank/com.example.bilal.bloodbank.Donor}: java.lang.NullPointerException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NullPointerException
                                                     at com.example.bilal.bloodbank.Donor.onCreate(Donor.java:24)
                                                     at android.app.Activity.performCreate(Activity.java:5397)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                     at dalvik.system.NativeStart.main(Native Method) 
01-19 00:10:04.741 4186-4186/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.example.bilal.bloodbank
01-19 00:10:04.761 4186-4186/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.bilal.bloodbank, PID: 4186
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilal.bloodbank/com.example.bilal.bloodbank.Donor}: java.lang.NullPointerException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NullPointerException
                                                     at com.example.bilal.bloodbank.Donor.onCreate(Donor.java:24)
                                                     at android.app.Activity.performCreate(Activity.java:5397)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                     at dalvik.system.NativeStart.main(Native Method) 
01-19 00:10:11.361 1773-4205/system_process E/ActivityManager: Exception in bstSendTopActivityInfo while sending HttpPost: Connect to /10.0.2.2:2861 timed out

你的帮助会得到高度赞赏 感谢

1 个答案:

答案 0 :(得分:2)

LogCat条目的重要部分是

  

引起:java.lang.NullPointerException at   com.example.bilal.bloodbank.Donor.onCreate(Donor.java:24)

Donor.java第24行

String Districts = (String)getIntent().getExtras().get(Extra_Disctrict);

试图让Districts脱离Intent extras Bundle,但getExtras()返回null。您的主要活动代码不会为意图添加任何额外内容:

String Districts = sp1.getSelectedItem().toString();
String bloodtypes = sp2.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Donor.class);
startActivity(intent);

在调用startActivity之前,将Strings添加到intent extras.

intent.putExtra("Districts", Districts);
intent.putExtra("bloodtypes", bloodtypes);