2014-07-09 173 views
0

我的應用程序意外停止。這是我的代碼。應用程序停止sqlite不工作

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

}

,這我的日誌貓。

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 

它不工作。如何糾正它。請儘可能儘快回覆。 它不起作用。如何糾正它。請儘可能儘快回覆。 它不起作用。如何糾正它。請儘可能儘快回覆。

+0

閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work因爲你是新的到stackoverflow – Raghunandan

回答

0

設置是這樣的:

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

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