简体   繁体   中英

Android Studio 1.3.1 error while Creating SQLite database

I am getting error in 26 line of MainActivity.java. There is showing JavaNULLPOINTER Exception. Please let me know what I should do to remove it. My code is on GitHub. https://github.com/happyshravan/SQLite

LogCat error (latest)

2-25 21:10:58.729  15661-15661/abcd.shravankr.sqlite D/memalloc﹕ /dev/pmemMapped buffer base:0x530b2000 size:10047488 offset:8511488 fd:66
12-25 21:11:06.679  15661-15661/abcd.shravankr.sqlite D/AndroidRuntime﹕  Shutting down VM
12-25 21:11:06.679  15661-15661/abcd.shravankr.sqlite W/dalvikvm﹕ threadid=1:   thread exiting with uncaught exception (group=0x40c15a68)
12-25 21:11:06.699  15661-15661/abcd.shravankr.sqlite E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
        at android.view.View.performClick(View.java:3571)
        at android.view.View$PerformClick.run(View.java:14247)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4517)
        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:993)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at 

I am getting error in 26 line of MainActivity.java

Line 26 refer to public void addButtonClicked(View view)

You have added the two button in xml , with id addButton and deleteButton but didn't use them in MainActivity .

There is showing JavaNULLPOINTER Exception

This is because you wanted to use these two button without initialize them.

Solution :

Try add this in onCreate

Button addBtn=(Button)findViewById(R.id.addButton);
Button deleteBtn=(Button)findViewById(R.id.deleteButton);

Edited

Remove android:onClick="addButtonClicked" and android:onClick="deleteButtonClicked" from main_activity and delete addButtonClicked and deleteButtonClicked in MainActivity . Use below code.

MainActivity

 addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //   add button is clicked
            Toast.makeText(getApplicationContext(),"Shravan",Toast.LENGTH_LONG).show();
            Products product = new Products(shravansInput.getText().toString());
            dbHandler.addProduct(product);
            printDatabase();
            }
        });


  deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { //   delete button is clicked
            String inputText=shravansInput.getText().toString();
            dbHandler.deleteProduct(inputText);
            printDatabase();
        });

put this two in onCreate method.

I hope this will help.

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