我的应用意外停止了。这是我的代码。
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 :(得分:2)
这个
Button add_new=(Button)findViewById(R.id.button1);
必须在onCreate
setContentView(R.layout.activity_main);
查看来源@ 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 }
答案 1 :(得分:1)
onCrete(){
setContentView(R.layout.activity_main);
Button add_new=(Button)findViewById(R.id.button1);//this event must be after your layout
...
...
}
答案 2 :(得分: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();
}
});