TXL2Activity
package and.roid;import and.roid.dbhelper.DBhelper;
import and.roid.entry.User;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class TXL2Activity extends Activity {
    /** Called when the activity is first created. */
EditText et_name;
EditText et_mobilephone;
EditText et_familyphone;
EditText et_officephone;
EditText et_position;
EditText et_company;
EditText et_address;
EditText et_zipcode;
EditText et_email;
EditText et_othercontact;
EditText et_re;
Button btn_save;
Button btn_return;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnews);
        
        
        btn_save.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=et_name.getText().toString();
if(name.equals("")){
Toast.makeText(TXL2Activity.this, "姓名不能为空", Toast.LENGTH_LONG).show();
}
String mobilephone=et_mobilephone.getText().toString();
String familyphone=et_familyphone.getText().toString();
String officephone=et_officephone.getText().toString();
String position=et_position.getText().toString();
String company=et_company.getText().toString();
String address=et_address.getText().toString();
String zipcode=et_zipcode.getText().toString();
String othercontact=et_othercontact.getText().toString();
String re=et_re.getText().toString();
String email=et_email.getText().toString();

User user=new User();
user.name=name;
user.mobilephone=mobilephone;
user.familyphone=familyphone;
user.officephone=officephone;
user.position=position;
user.company=company;
user.address=address;
user.zipcode=zipcode;
user.othercontact=othercontact;
user.re=re;
user.email=email;

DBhelper.getInstance(TXL2Activity.this).save(user);
}});
    }
    
    public void initwidget(){
     et_name=(EditText) this.findViewById(R.id.username);
     et_mobilephone=(EditText) this.findViewById(R.id.mobilephone);
     et_familyphone=(EditText) this.findViewById(R.id.familyphone);
     et_officephone=(EditText) this.findViewById(R.id.officephone);
     et_position=(EditText) this.findViewById(R.id.position);
     et_company=(EditText) this.findViewById(R.id.company);
     et_address=(EditText) this.findViewById(R.id.address);
     et_zipcode=(EditText) this.findViewById(R.id.zipcode);
     et_email=(EditText) this.findViewById(R.id.email);
     et_othercontact=(EditText) this.findViewById(R.id.othercontact);
     et_re=(EditText) this.findViewById(R.id.re);
    
     btn_save=(Button) this.findViewById(R.id.btn_save);
     btn_return=(Button) this.findViewById(R.id.btn_return);
    
    }
}
DBHelper
package and.roid.dbhelper;import and.roid.entry.User;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;public class DBhelper extends SQLiteOpenHelper{
private final static String DB_NAME="ceshi";
private static int Version=1;
private static DBhelper instance=null;
    private SQLiteDatabase db;

public void openDatabase(){
if(db==null)
db=this.getWritableDatabase();

}
    public static DBhelper getInstance(Context context){
     if(instance==null){
     instance=new DBhelper(context);
    
     }
     return instance;
    
    
    }
    public DBhelper(Context context) {
super(context, DB_NAME, null, Version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
StringBuffer creatdb=new StringBuffer();
creatdb.append("creat table user(_id integer primary key autoincrement,")
        .append("name text,")
        .append("moblephone text,")
        .append("familyphone text,")
        .append("officephone text,")
        .append("position text,")
        .append("company text,")
        .append("address text,")
        .append("email text,")
        .append("zipcode text,")
        .append("othercontact text,")
        .append("re text)");
db.execSQL(creatdb.toString());


} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String updatasql="drop table if exists user";
db.execSQL(updatasql);
onCreate(db);

}
public void save(User user){
ContentValues values=new ContentValues();
values.put("name", user.name);
values.put("moblephone",user.mobilephone);
values.put("familyphone", user.familyphone);
values.put("officephone", user.officephone);
values.put("position", user.position);
values.put("company", user.company);
values.put("address", user.address);
values.put("email", user.email);
values.put("zipcode", user.zipcode);
values.put("othercontact", user.othercontact);
values.put("re", user.re);

db.insert("user", null, values);


}}user
package and.roid.entry;public class User {
public int _id;
public String name;
public String mobilephone;
public String familyphone;
public String officephone;
public String address;
public String zipcode;
public String othercontact;
public String re;
public String email;
public String position;
public String company;

}

解决方案 »

  1.   

    最后就是运行不起来,logcat内容是:\
    11-03 02:12:34.743: DEBUG/AndroidRuntime(748): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    11-03 02:12:34.743: DEBUG/AndroidRuntime(748): CheckJNI is ON
    11-03 02:12:35.086: DEBUG/AndroidRuntime(748): --- registering native functions ---
    11-03 02:12:35.105: INFO/jdwp(748): received file descriptor 20 from ADB
    11-03 02:12:35.523: DEBUG/ddm-heap(748): Got feature list request
    11-03 02:12:36.305: DEBUG/AndroidRuntime(748): Shutting down VM
    11-03 02:12:36.305: DEBUG/dalvikvm(748): DestroyJavaVM waiting for non-daemon threads to exit
    11-03 02:12:36.305: DEBUG/dalvikvm(748): DestroyJavaVM shutting VM down
    11-03 02:12:36.313: DEBUG/dalvikvm(748): HeapWorker thread shutting down
    11-03 02:12:36.313: DEBUG/dalvikvm(748): HeapWorker thread has shut down
    11-03 02:12:36.313: DEBUG/jdwp(748): JDWP shutting down net...
    11-03 02:12:36.335: DEBUG/jdwp(748): +++ peer disconnected
    11-03 02:12:36.335: INFO/dalvikvm(748): Debugger has detached; object registry had 1 entries
    11-03 02:12:36.343: DEBUG/dalvikvm(748): VM cleaning up
    11-03 02:12:36.353: DEBUG/dalvikvm(748): LinearAlloc 0x0 used 665652 of 4194304 (15%)
    11-03 02:12:37.072: DEBUG/AndroidRuntime(758): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    11-03 02:12:37.072: DEBUG/AndroidRuntime(758): CheckJNI is ON
    11-03 02:12:37.452: DEBUG/AndroidRuntime(758): --- registering native functions ---
    11-03 02:12:37.462: INFO/jdwp(758): received file descriptor 20 from ADB
    11-03 02:12:37.904: DEBUG/ddm-heap(758): Got feature list request
    11-03 02:12:38.692: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=and.roid/.TXL2Activity }
    11-03 02:12:38.753: DEBUG/AndroidRuntime(758): Shutting down VM
    11-03 02:12:38.753: DEBUG/dalvikvm(758): DestroyJavaVM waiting for non-daemon threads to exit
    11-03 02:12:38.763: DEBUG/dalvikvm(758): DestroyJavaVM shutting VM down
    11-03 02:12:38.763: DEBUG/dalvikvm(758): HeapWorker thread shutting down
    11-03 02:12:38.773: DEBUG/dalvikvm(758): HeapWorker thread has shut down
    11-03 02:12:38.773: DEBUG/jdwp(758): JDWP shutting down net...
    11-03 02:12:38.782: DEBUG/jdwp(758): +++ peer disconnected
    11-03 02:12:38.782: INFO/dalvikvm(758): Debugger has detached; object registry had 1 entries
    11-03 02:12:38.853: DEBUG/dalvikvm(758): VM cleaning up
    11-03 02:12:38.933: DEBUG/dalvikvm(758): LinearAlloc 0x0 used 676828 of 4194304 (16%)
    11-03 02:12:38.983: INFO/ActivityManager(52): Start proc and.roid for activity and.roid/.TXL2Activity: pid=766 uid=10024 gids={}
    11-03 02:12:39.072: INFO/jdwp(766): received file descriptor 20 from ADB
    11-03 02:12:39.183: DEBUG/ddm-heap(766): Got feature list request
    11-03 02:12:39.872: DEBUG/AndroidRuntime(766): Shutting down VM
    11-03 02:12:39.872: WARN/dalvikvm(766): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
    11-03 02:12:39.872: ERROR/AndroidRuntime(766): Uncaught handler: thread main exiting due to uncaught exception
    11-03 02:12:39.892: ERROR/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{and.roid/and.roid.TXL2Activity}: java.lang.NullPointerException
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.os.Looper.loop(Looper.java:123)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread.main(ActivityThread.java:4203)11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at java.lang.reflect.Method.invoke(Method.java:521)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at dalvik.system.NativeStart.main(Native Method)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766): Caused by: java.lang.NullPointerException
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at and.roid.TXL2Activity.onCreate(TXL2Activity.java:34)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
    11-03 02:12:39.892: ERROR/AndroidRuntime(766):     ... 11 more
    11-03 02:12:39.913: INFO/Process(52): Sending signal. PID: 766 SIG: 3
    11-03 02:12:39.925: INFO/dalvikvm(766): threadid=7: reacting to signal 3
    11-03 02:12:39.973: INFO/dalvikvm(766): Wrote stack trace to '/data/anr/traces.txt'
    11-03 02:12:48.770: WARN/ActivityManager(52): Launch timeout has expired, giving up wake lock!
    11-03 02:12:49.403: WARN/ActivityManager(52): Activity idle timeout for HistoryRecord{438cb0a8 and.roid/.TXL2Activity}
      

  2.   

    已经解决了,花点时间研究一下logcat就看出来了
    11-03 02:12:39.892: ERROR/AndroidRuntime(766): Caused by: java.lang.NullPointerException
    11-03 02:12:39.892: ERROR/AndroidRuntime(766): at and.roid.TXL2Activity.onCreate(TXL2Activity.java:34)
    说明有东西没有初始化..然后一查就看出来了!很简单的!!