sammalik
sammalik

Reputation: 83

application stopped sqlite not working

my app is stopped unexpectedly. this is my code.

package com.nisani.angel;

//import info.androidhive.jsonparsing.MainActivity.GetContacts;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log; 
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
 import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText edit_name;
EditText edit_phone;
Button add_new=(Button)findViewById(R.id.button1);
SQLiteDatabase db=null;
String tag=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(tag, "1");
    try{

       db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
       db.execSQL("create table if not exists myname(name varchar,mobile_no varchar)");

    }

    catch(NullPointerException e){

        Log.i(tag, "2");
    }
add_new.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        edit_phone=(EditText)findViewById(R.id.phone);
        edit_name=(EditText)findViewById(R.id.name);
        String name1=edit_name.getText().toString();
        String mobile_no1=edit_phone.getText().toString();
        db.execSQL("insert into myname values('"+name1+"','"+mobile_no1+"')");
        db.close();

    }
});

Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
            MenuItem item = menu.findItem(R.id.menu_item_share);

            ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();

            Intent myIntent = new Intent();
            myIntent.setAction(Intent.ACTION_SEND);
            myIntent.putExtra(Intent.EXTRA_TEXT, " ");
            myIntent.setType("text/plain");

            myShareActionProvider.setShareIntent(myIntent);

            return true;
        }

}

and this my log cat.

 07-09 13:22:54.010: D/AndroidRuntime(2251): Shutting down VM
 07-09 13:22:54.010: W/dalvikvm(2251): threadid=1: thread exiting with     uncaught           exception (group=0xb2a96ba8)
 07-09 13:22:54.060: E/AndroidRuntime(2251): FATAL EXCEPTION: main
07-09 13:22:54.060: E/AndroidRuntime(2251): Process: com.nisani.angel, PID: 2251
07-09 13:22:54.060: E/AndroidRuntime(2251): java.lang.RuntimeException: Unable to     instantiate activity ComponentInfo{com.nisani.angel/com.nisani.angel.MainActivity}: java.lang.NullPointerException
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.os.Looper.loop(Looper.java:136)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at java.lang.reflect.Method.invoke(Method.java:515)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at dalvik.system.NativeStart.main(Native Method)
07-09 13:22:54.060: E/AndroidRuntime(2251): Caused by: java.lang.NullPointerException
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.Activity.findViewById(Activity.java:1884)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at com.nisani.angel.MainActivity.<init>(MainActivity.java:21)
 07-09 13:22:54.060: E/AndroidRuntime(2251):    at     java.lang.Class.newInstanceImpl(Native Method)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at     java.lang.Class.newInstance(Class.java:1208)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-09 13:22:54.060: E/AndroidRuntime(2251):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-09 13:22:54.060: E/AndroidRuntime(2251):     ... 11 more
07-09 13:23:02.440: I/Process(2251): Sending signal. PID: 2251 SIG: 9

it is not working. how to correct it. please reply as soon as possible. it is not working. how to correct it. please reply as soon as possible. it is not working. how to correct it. please reply as soon as possible.

Upvotes: 0

Views: 61

Answers (3)

Meghna
Meghna

Reputation: 537

onCrete(){ 
setContentView(R.layout.activity_main);
Button add_new=(Button)findViewById(R.id.button1);//this event must be after your layout
...
...
}

Upvotes: 1

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Set like this :

public class MainActivity extends Activity {
 EditText edit_name;
 EditText edit_phone;
 Button add_new;
 SQLiteDatabase db=null;
 String tag=null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  add_new=(Button)findViewById(R.id.button1);     // Add here
  Log.i(tag, "1");
  try{

   db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
   db.execSQL("create table if not exists myname(name varchar,mobile_no varchar)");

  }

  catch(NullPointerException e){

    Log.i(tag, "2");
  }
 add_new.setOnClickListener(new View.OnClickListener() {

 @Override
 public void onClick(View arg0) {
    edit_phone=(EditText)findViewById(R.id.phone);
    edit_name=(EditText)findViewById(R.id.name);
    String name1=edit_name.getText().toString();
    String mobile_no1=edit_phone.getText().toString();
    db.execSQL("insert into myname values('"+name1+"','"+mobile_no1+"')");
    db.close();

 }
});

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

This

 Button add_new=(Button)findViewById(R.id.button1);

must be in onCreate after setContentView(R.layout.activity_main);

Looking at the source @ http://androidxref.com/4.4.3_r1.1/xref/frameworks/base/core/java/android/app/Activity.java

1883    public View findViewById(int id) {
1884        return getWindow().findViewById(id); // returns null
1885    }

Upvotes: 2

Related Questions