NullPoint启动时的活动

时间:2016-03-26 16:25:14

标签: android

我有1个问题,谁可以帮助我? 我在启动App时运行了1个Activity

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

然后它将IdUser发送到此活动

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

}

当我第一次运行它时,这个应用程序没问题,它可以转到下一个活动

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

但是当我第二次运行时,当我点击按钮打开活动时 InsertNote,我收到错误

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) 

我尝试将我从Activity Main发送的内容打印到Toast的Activity ManagePage,它显示我要发送的内容。 但我不知道为什么会出现这个错误。 你能帮助我吗? 抱歉我的英文

2 个答案:

答案 0 :(得分:1)

您在InsertNote活动的setBtnCancelNote()方法中调用onCreate()方法。因此,只要InsertNote启动,它就会尝试启动ManagePage活动。

问题是您的ManagePage活动期望从intent接收的包中的值IdUser。但是,当您从InsertNote活动启动时,您不会将此值传递给ManagePage活动。

您可能希望将此行添加到InsertPage活动中的setBtncancelNote()方法 -

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

如果您传递此值,则ManageNote不会崩溃。

答案 1 :(得分:0)

请添加更多信息,如行号,否则很难远程调试..

无论如何,我猜你在做像getIntent()之类的东西时必须检查空指针.getExtras()

只是在那里放一些断点,然后一步一步地弄清楚