2016-03-26 26 views
-1

我有1個問題,誰能幫助我? 我有1個活動運行時啓動應用程序NullPoint when start活動

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

然後將其發送ID用戶到這個活動

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)  

我試着打印我從活動主頁發送到Toast的活動管理頁面,它顯示了我想要發送的內容。 但我不知道爲什麼會顯示此錯誤。 你能幫我嗎? 對不起我的英語

+0

你可以告訴這行代碼這個'在com.example.khuatduytan.doantotnghiep.ManagePage.onCreate(ManagePage.java:21 ' – Kathi

+0

它是 Bundle extras = getIntent()。getExtras(); final String idUs er = extras.getString(「IdUser」); –

回答

1

您可以在您的InsertNote活動的onCreate()方法中調用setBtnCancelNote()方法。所以一旦InsertNote啓動,它就會嘗試啓動ManagePage活動。

問題是您的ManagePage活動期望從意圖收到的包中的值IdUser。但是,當您從InsertNote活動啓動時,您不會將此值傳遞給ManagePage活動。

你可能想這行添加到您的setBtncancelNote()方法InsertPage活動 -

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

如果你通過這個值,那麼ManageNote不會崩潰。

+0

我將此行添加到了我的方法中,但是當我點擊BtnSubmit時,Activity ManageNote會顯示。我必須點擊返回以顯示活動插入筆記。你能幫我解決這個問題嗎 –

+0

請閱讀我的答案的前兩行 –

+0

感謝您的幫助 –

0

請添加更多的信息像行號,否則其很難遠程調試吧..

無論如何,我的猜測是,你必須檢查空指針,當你做的東西一樣getIntent()。getExtras()

只是把一些斷點那裏,一步一步,你就看着辦吧