这是我实施onItemClickListener
并开始新活动的课程。根据logcat,当我开始NullPointerException
时,GetAppoint
正在发生。
public class Sdoctors extends Fragment implements OnItemSelectedListener {
public String itemselect = "";
public doctorsDBadapter dbhelper;
public SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.sdoctors, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
List<String> categories = new ArrayList<String>();
categories.add("By Name");
categories.add("By Speciality");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
dbhelper = new doctorsDBadapter(getActivity());
dbhelper.open();
dbhelper.deletealldoctors();
dbhelper.insertsomedoctors();
final Cursor cursor = dbhelper.fetchalldoctors();
String[] columns = new String[] { doctorsDBadapter.KEYNAME,
doctorsDBadapter.KEYSPEC, doctorsDBadapter.KEYTIME };
int[] to = new int[] { R.id.name, R.id.speac, R.id.time };
dataAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.doctorsinfo, cursor, columns, to, 0);
final ListView listview = (ListView) rootView
.findViewById(R.id.dlistView1);
listview.setAdapter(dataAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) listview.getItemAtPosition(position);
String doctor = cursor.getString(cursor
.getColumnIndexOrThrow("_name"));
String spec = cursor.getString(cursor
.getColumnIndexOrThrow("_specialist"));
String time = cursor.getString(cursor
.getColumnIndexOrThrow("_time"));
Log.i("DOCTOR", doctor);
Log.i("SPECIALIST", spec);
Log.i("TIME",time);
Intent openApp = new Intent(view.getContext(), GetAppoint.class);
openApp.putExtra("doct", doctor);
openApp.putExtra("specs", spec);
openApp.putExtra("times", time);
startActivity(openApp);
}
});
EditText myfilter = (EditText) rootView.findViewById(R.id.editText1);
myfilter.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
dataAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// TODO Auto-generated method stub
if (itemselect == "By Speciality") {
return dbhelper.fetchdoctorsbyspec(constraint.toString());
} else if (itemselect == "By Name") {
return dbhelper.fetchdoctorsbyname(constraint.toString());
}
return cursor;
}
});
return rootView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
itemselect = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}}
这是新的活动类GetAppoint.java
public class GetAppoint extends Activity{
ActionBar actiobar;
TextView name,spec,time;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getappoint);
name = (TextView) findViewById(R.id.textView2);
spec = (TextView) findViewById(R.id.textview4);
time = (TextView) findViewById(R.id.textView5);
actiobar = getActionBar();
actiobar.setHomeButtonEnabled(true);
actiobar.setDisplayHomeAsUpEnabled(true);
String doc = getIntent().getExtras().getString("doct");
String specialist = getIntent().getExtras().getString("specs");
String doctime = getIntent().getExtras().getString("times");
name.append(doc);
spec.append(specialist);
time.append(doctime);
}}
这是xml文件getappoint.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView7" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:layout_toLeftOf="@+id/textView7"
android:text="Specialist"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/textView5"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignLeft="@+id/textView4"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignRight="@+id/textView3"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/textView5"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/textView4"
android:layout_marginLeft="26dp"
android:layout_marginTop="14dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Select Date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/appointbut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker1"
android:text="Confirm" />
</RelativeLayout>
这是我的Logcat
03-07 00:22:53.770: E/AndroidRuntime(10342): FATAL EXCEPTION: main
03-07 00:22:53.770: E/AndroidRuntime(10342): Process: com.beproject.pocketdoctor, PID: 10342
03-07 00:22:53.770: E/AndroidRuntime(10342): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beproject.pocketdoctor/com.beproject.pocketdoctor.GetAppoint}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread.access$800(ActivityThread.java:148)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.os.Looper.loop(Looper.java:135)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread.main(ActivityThread.java:5312)
03-07 00:22:53.770: E/AndroidRuntime(10342): at java.lang.reflect.Method.invoke(Native Method)
03-07 00:22:53.770: E/AndroidRuntime(10342): at java.lang.reflect.Method.invoke(Method.java:372)
03-07 00:22:53.770: E/AndroidRuntime(10342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-07 00:22:53.770: E/AndroidRuntime(10342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-07 00:22:53.770: E/AndroidRuntime(10342): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
03-07 00:22:53.770: E/AndroidRuntime(10342): at com.beproject.pocketdoctor.GetAppoint.onCreate(GetAppoint.java:31)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.Activity.performCreate(Activity.java:5953)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
03-07 00:22:53.770: E/AndroidRuntime(10342): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
答案 0 :(得分:1)
我想知道为什么你没有得到编译时错误。你的R本身不应该包含它,所以应该显示错误。
这是你的错误
spec = (TextView) findViewById(R.id.textview4); // text'v'iew4 'v' is in small letter
你有
android:id="@+id/textView4"
因为你的TextView引用了null,因为找不到元素,而且你正在调用
.append()就此而言,给出错误
03-07 00:22:53.770:E / AndroidRuntime(10342):引起: java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.TextView.append(java.lang.CharSequence中)&#39;在null 对象参考
答案 1 :(得分:0)
在onCreate()
活动的GetAppoint
中,您从意图中获取了包,因为您未设置任何意图,因此该空包为空。改为:
String doc = getIntent().getStringExtra("doct");
...