-1

I have 1 problem and Who can help me?. I have 1 Activity run when start App

package com.example.khuatduytan.doantotnghiep;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.Serializable;

public class MainActivity extends AppCompatActivity {
    DatabaseHelper db;
    Button btnLogin, btnRegister, btnFindPassword;
    EditText editUsername, editPassword;
    private static final int REQUEST_CODE = 10;

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

        db = new DatabaseHelper(this);
        btnLogin = (Button) findViewById(R.id.button_login);
        btnRegister = (Button) findViewById(R.id.button_register);
        btnFindPassword = (Button) findViewById(R.id.button_findPassword);
        editUsername = (EditText) findViewById(R.id.editText_username);
        editPassword = (EditText) findViewById(R.id.editText_password);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarActivity);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Đăng nhập");

        Login();
        Register();
        FindPassword();

    }

    public void Login(){
        btnLogin.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Cursor res = db.getDataTableUser();
                        int temp = 1;
                        if (res.getCount() == 0) {
                            showMessage("Error", "Tài khoản không tồn tại");
                            return;
                        }

                        while (res.moveToNext()) {
                            String username, password, idUser;
                            idUser = res.getString(0);
                            username = res.getString(1);
                            password = res.getString(2);
                            if (editUsername.getText().toString().equals(username) == true&&editPassword.getText().toString().equals(password) == true) {
                                doOpenManagePage(idUser);
                                temp = 0;
                                break;
                            }
                        }
                        if (temp==1){
                            showMessage("Error", "Account does not exist");
                        }
                    }
                }
        );
    }

    public void Register(){
        btnRegister.setOnClickListener(
                new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        doOpenRegister();
                    }
                }
        );
    }

    public void FindPassword(){
        btnFindPassword.setOnClickListener(
                new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        doOpenFindPasswordStep1();
                    }
                }
        );
    }

    public void doOpenRegister(){
        Intent newIntent = new Intent(this, Register.class);
        startActivity(newIntent);
    }

    public void doOpenFindPasswordStep1(){
        Intent newIntent = new Intent(this, FindPasswordStep1.class);
        startActivity(newIntent);
    }

    public void doOpenManagePage(String idUser){
        Intent newIntent = new Intent(this, ManagePage.class);
        newIntent.putExtra("IdUser", idUser);
        startActivityForResult(newIntent, REQUEST_CODE);
    }



    public void showMessage(String title, String Message){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
    }

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_manage_page, menu);
            return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            return true;
        }
        else if (id==R.id.action_search){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Then it send IdUser to this Activity

package com.example.khuatduytan.doantotnghiep;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class ManagePage extends AppCompatActivity{
    private static final int REQUEST_CODE = 10;

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_note);
        Bundle extras = getIntent().getExtras();
        final String idUser = extras.getString("IdUser");
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doOpenInsertNote(idUser);
            }
        });
    }

    public String getIdUser(){
        Bundle extras = getIntent().getExtras();
        String idUser = extras.getString("IdUser");
        return idUser;
    }

    public void doOpenInsertNote(String idUser){
        Intent newIntent = new Intent(this, InsertNote.class);
        newIntent.putExtra("IdUser", idUser);
        startActivityForResult(newIntent, REQUEST_CODE);
    }

}

When i run it 1st time, this app is ok, it can go to next Activity

package com.example.khuatduytan.doantotnghiep;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Calendar;

public class InsertNote extends AppCompatActivity implements View.OnClickListener {

    private ImageButton insertDate;
    private Calendar cal;
    private int day;
    private int month;
    private int year;
    private EditText et, content_InsertNote, money_InsertNote;
    private Button btnSubmitNote, btnCancelNote;
    private Spinner SelectTypeNote;
    DatabaseHelper db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_note);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        insertDate = (ImageButton) findViewById(R.id.dateInsert);
        cal = Calendar.getInstance();
        et = (EditText) findViewById(R.id.dateInsert_editText);
        btnSubmitNote = (Button) findViewById(R.id.insertNote);
        btnCancelNote = (Button) findViewById(R.id.cancelNote);
        content_InsertNote = (EditText) findViewById(R.id.noiDung);
        money_InsertNote = (EditText) findViewById(R.id.soTien);
        SelectTypeNote = (Spinner) findViewById(R.id.loai);

        db = new DatabaseHelper(this);

        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
        insertDate.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();
        final String idUser = extras.getString("IdUser");

        setBtnSubmitNote(idUser);
        setBtnCancelNote();
    }

    @Override
    public void onClick(View v) {
        showDialog(0);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        return new DatePickerDialog(this, datePickerListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            et.setText(selectedDay + " / " + (selectedMonth + 1) + " / " + selectedYear);
        }
    };

    private void setBtnSubmitNote(final String idUser){
        btnSubmitNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmDialog(idUser);
            }
        });
    }

    private void setBtnCancelNote(){
        Intent newIntent = new Intent(this, ManagePage.class);
        startActivity(newIntent);
    }

    private void confirmDialog(final String idUser) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder
                .setMessage("Are you sure?")
                .setPositiveButton("Yes",  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        String date = null, content = null, money = null, TypeNote_Selected;
                        int Type_Note = 0, id_User;

                        id_User = Integer.parseInt(idUser);
                        date = et.getText().toString();
                        content = content_InsertNote.getText().toString();
                        money = money_InsertNote.getText().toString();
                        TypeNote_Selected = SelectTypeNote.getSelectedItem().toString();
                        if(TypeNote_Selected.equals("Thu")){
                            Type_Note = 0;
                        } else{
                            Type_Note = 1;
                        }

                        if(date.equals("")||content.equals("")||money.equals("")){
                            Toast.makeText(InsertNote.this, "Bạn chưa nhập đủ dữ liệu", Toast.LENGTH_LONG).show();
                        }
                        else {
                            long isInserted = db.insertNoteTable(date, content, Type_Note, money, id_User);
                            if (isInserted == -1) {
                                Toast.makeText(InsertNote.this, "Thêm ghi chú không thành công", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(InsertNote.this, "Thêm ghi chú thành công", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                })
                .show();
    }
}
`

But when i run it second time, When i click button to open Activity InsertNote, I receive a error

03-26 16:12:32.161 2614-2614/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.khuatduytan.doantotnghiep/com.example.khuatduytan.doantotnghiep.ManagePage}: java.lang.NullPointerException
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: java.lang.NullPointerException
                                                 at com.example.khuatduytan.doantotnghiep.ManagePage.onCreate(ManagePage.java:21)
                                                 at android.app.Activity.performCreate(Activity.java:5104)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                 at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                 at android.os.Looper.loop(Looper.java:137) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:511) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                 at dalvik.system.NativeStart.main(Native Method) 

I tried to print what i send from Activity Main to Activity ManagePage by Toast, it display exactly what i want to send. But i dont know why this error display. Can you help me? Sorry about my English

2 Answers2

1

You call setBtnCancelNote() method in the onCreate() method of your InsertNote activity. so as soon as InsertNote starts it tries to launch the ManagePage activity.

The problem is your ManagePage activity expects the value IdUser in the bundle received from the intent. But you do not pass this value to ManagePage activity when you start it from InsertNote activity.

You probably want to add this line to your setBtncancelNote() method in InsertPage activity -

newIntent.putExtra("IdUser", //The Values here);

If you pass this value then ManageNote will not crash.

Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
0

please add more info like line numbers, otherwise its difficult to debug it remotely ..

Anyway, my guess is you have to check for null pointers when you do stuff like getIntent().getExtras()

just put some breakpoints there, and step by step and you'll figure it out

Sergio Lima
  • 114
  • 13