从一个活动移动到另一个活动时,它显示空指针异常,但我找不到错误发生的位置。
在下面给出我的 logcat
10-04 12:14:10.876: E/AndroidRuntime(1966): FATAL EXCEPTION: main
10-04 12:14:10.876: E/AndroidRuntime(1966): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.neochat/com.neochat.Displayfriendlist}:
java.lang.NullPointerException
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.os.Looper.loop(Looper.java:137)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-04 12:14:10.876: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 12:14:10.876: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 12:14:10.876: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-04 12:14:10.876: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-04 12:14:10.876: E/AndroidRuntime(1966): at dalvik.system.NativeStart.main(Native Method)
10-04 12:14:10.876: E/AndroidRuntime(1966): Caused by: java.lang.NullPointerException
10-04 12:14:10.876: E/AndroidRuntime(1966): at com.neochat.LoginDataBaseAdapter.Displayfriend(LoginDataBaseAdapter.java:110)
10-04 12:14:10.876: E/AndroidRuntime(1966): at com.neochat.Displayfriendlist.onCreate(Displayfriendlist.java:25)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.Activity.performCreate(Activity.java:5104)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-04 12:14:10.876: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-04 12:14:10.876: E/AndroidRuntime(1966): ... 11 more
在下面给出我的java类
public class DisplayFriendList extends Activity {
Context context = this;
LoginDataBaseAdapter Logi;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.displayfriend);
Logi = new LoginDataBaseAdapter(this);
Bundle data = getIntent().getExtras();
String code = data.getString("EMPCode");
String gotfriend = Logi.Displayfriend(code);
TextView txt = (TextView) findViewById(R.id.getfriend);
txt.setText(gotfriend);
Button btnadd = (Button) findViewById(R.id.data_addfriend);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Logi.insertFriendEntry(String Name, String Emp_Code);
Toast.makeText(context, "Friend added successfully",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
}
在 LoginDataBaseAdapter 类中给出函数 Displayfriend ..
public String DisplayFriend(String Emp_Code) {
String displayfriend = "SELECT NAME , EMPLOYEE_CODE FROM EMPLOYEES " +
"WHERE " + " EMPLOYEE_CODE " + "LIKE '" +Emp_Code;
Cursor display = db.rawQuery(displayfriend, null);
String data = display.getString(0);
return data;
}