简体   繁体   中英

My android application closes when moving to another activity

i'm new here and new to programming in general

I'm creating a blood donation android application with SQLite and when i move to another activity to display the donors it closes.

I created a database using DB Helper Here is the code:

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

My main activity consists of two spinner to specify the district and blood type and a button to move to the activity

Here is the Code for the main activity

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

    }
}

Now when the button is pressed a new activity should start displaying the donors but the application crashes Here is the code

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

and here is the 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

Your help would be highly Appreciated Thanks

The important part of your LogCat entry is

Caused by: java.lang.NullPointerException at com.example.bilal.bloodbank.Donor.onCreate(Donor.java:24)

Line 24 of Donor.java

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

is trying to get Districts out of the Intent extras Bundle but getExtras() is returning null. Your main activity code is not adding any extras to the intent:

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

Add your Strings to the intent extras before calling startActivity.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM