我创建了一个预约页面,其中包括按钮即添加预约和主页。但是当我尝试点击此预约选项时,应用程序停止..无法找到任何错误...请帮助
Tab1.java
package com.example.app;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.content.Intent;
public class Tab1 extends Activity
{
private ListView infoListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
// Find the ListView resource.
infoListView = (ListView) findViewById( R.id.infoListView );
// Create and populate a List of planet names.
String[] person = new String[] { "Persons","Encounter", "Medications","Weight","BloodPressure",
"BloodSugar","Lab and Test Results","Office Visit","Office Info","Data Analysis","Appointment","Allergy","Problem"};
ArrayList<String> personList = new ArrayList<String>();
personList.addAll( Arrays.asList(person) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.inforow, personList);
// Set the ArrayAdapter as the ListView's adapter.
infoListView.setAdapter( listAdapter );
/* infoListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Intent i3=new Intent(Info.this,Call.class);
startActivity(i3);
}
});*/
infoListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
switch (position)
{
case 0:
Intent i1= new Intent (Tab1.this ,Person.class);
startActivity(i1);
break;
case 1 :
Intent i2 = new Intent (Tab1.this ,History.class);
startActivity(i2);
break;
case 2 :
Intent i3 = new Intent (Tab1.this , Medications.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent (Tab1.this , Weight.class);
startActivity(i4);
break;
case 4:
Intent i5 = new Intent (Tab1.this , BloodPressure.class);
startActivity(i5);
break;
case 5:
Intent i6 = new Intent (Tab1.this , BloodSugar.class);
startActivity(i6);
break;
case 6:
Intent i7 = new Intent (Tab1.this , LabAndTestResults.class);
startActivity(i7);
break;
case 7:
Intent i8 = new Intent (Tab1.this , OfficeVisit.class);
startActivity(i8);
break;
case 8:
Intent i9 = new Intent (Tab1.this , Office_info_button.class);
startActivity(i9);
break;
case 9:
Intent i10 = new Intent (Tab1.this ,SimpleListActivity.class);
startActivity(i10);
break;
case 10:
Intent i11 = new Intent (Tab1.this ,Appointment1.class);
startActivity(i11);
break;
case 11:
Intent i12 = new Intent (Tab1.this ,Allergy.class);
startActivity(i12);
break;
case 12:
Intent i13 = new Intent (Tab1.this ,ProblemButton.class);
startActivity(i13);
break;
}
}
});
}
}
Appointment1.java
package com.example.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.os.Build;
public class Appointment1 extends ActionBarActivity {
Button addApointment;
String []dateofAppointment;
String []physicianName;
Button homepage;
TableLayout appointment_table;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment1);
addApointment=(Button)findViewById(R.id.button1);
homepage=(Button)findViewById(R.id.button2);
System.out.println("in appintment1");
appointment_table=(TableLayout)findViewById(R.id.appointment_table);
AddAppointment obj=new AddAppointment();
obj.getAppointment();
dateofAppointment=obj.dateofAppointment;
physicianName=obj.physicianName;
fillCountryTable();
System.out.println("in appintment1 on create");
addApointment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent(Appointment1.this,AddAppointment.class);
startActivity(i);
}
});
homepage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i=new Intent(Appointment1.this,TabBar.class);
startActivity(i);
}
});
}
public void fillCountryTable() {
TableRow row;
TextView t1, t2;
//Converting to dip unit
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 1, getResources().getDisplayMetrics());
System.out.println("in appintment1 fill country");
for (int current = 0; current < dateofAppointment.length; current++) {
row = new TableRow(this);
t1 = new TextView(this);
// t1.setTextColor(getResources().getColor(R.color.yellow));
t2 = new TextView(this);
//t2.setTextColor(getResources().getColor(R.color.dark_red));
t1.setText(physicianName[current]);
t2.setText(dateofAppointment[current]);
t1.setTypeface(null, 1);
t2.setTypeface(null, 1);
t1.setTextSize(15);
t2.setTextSize(15);
t1.setWidth(15 * dip);
t2.setWidth(15 * dip);
t1.setPadding(0*dip, 0, 0, 0);
//t2.setPadding(0*dip, 0, 0, 0);
row.addView(t1);
row.addView(t2);
appointment_table.addView(row, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
}
addappointment.java
package com.example.app;
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class AddAppointment extends ActionBarActivity implements OnClickListener,
OnItemSelectedListener
{
private Button mSave;
private Button mDelete;
String []dateofAppointment=new String[100];
String []physicianName=new String[100];
private EditText mPhysicianName;
// Add Datepicker
private EditText tvDisplayDate;
//private DatePicker dpResult;
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
protected static DBHelper1 DB1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_appointment);
DB1=DBHelper1.getDB();
// Assignment of UI fields to the variables
mSave = (Button) findViewById(R.id.save);
mSave.setOnClickListener(this);
mDelete = (Button) findViewById(R.id.delete);
mDelete.setOnClickListener(this);
mPhysicianName = (EditText) findViewById(R.id.editText1);
tvDisplayDate = (EditText) findViewById(R.id.tvDate);
setCurrentDateOnView();
addListenerOnButton();
}
@Override
public void onClick(View v)
{
System.out.println("In click ");
System.out.println("PAtient id " +LoginActivity1.getPatientId());
switch (v.getId()) {
case R.id.delete:
Intent i = new Intent(getBaseContext(), Appointment.class);
startActivity(i);
// finish();
break;
case R.id.save:
System.out.println("rEGISTER BUTTON CLICK");
String DateofAppointment = tvDisplayDate.getText().toString();
String PhysicianName = mPhysicianName.getText().toString();
boolean invalid = false;
if (DateofAppointment.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Enter DateofAppointment",
Toast.LENGTH_SHORT).show();
} else
if (PhysicianName.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter PhysicianName", Toast.LENGTH_SHORT)
.show();
} else if (invalid == false)
{
//String DateofAppointment = null;
addEntry(DateofAppointment, PhysicianName);
getAppointment();
Intent i_register = new Intent(AddAppointment.this,
Appointment.class);
startActivity(i_register);
// finish();
}
break;
}
}
public void onDestroy() {
super.onDestroy();
//DB1.close();
}
private void addEntry(String DateofAppointment,String PhysicianName)
{
SQLiteDatabase db1 = DB1.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put("id", 2);
values.put("patientid", LoginActivity1.getPatientId());
values.put("DateofAppointment", DateofAppointment);
values.put("PhysicianName", PhysicianName);
try {
long rowId = db1.insert(DBHelper1.DATABASE_TABLE_NAME10, null, values);
System.out.println("rowId: "+rowId);
Toast.makeText(getApplicationContext(),
"your details submitted Successfully...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getAppointment()
{
DB1=DBHelper1.getDB();
String id=String.valueOf(LoginActivity1.getPatientId());
System.out.println("Patient id "+id);
System.out.println("in getAppointment---1");
SQLiteDatabase db1 = DB1.getReadableDatabase();
System.out.println("in getAppointment--3");
String[] columns = { "DateofAppointment,PhysicianName" };
String selection = "patientid=?";
String[] selectionArgs = { id };
Cursor cursor = null;
try {
cursor = db1.query(DBHelper1.DATABASE_TABLE_NAME10, columns, selection,
selectionArgs, null, null, null);
cursor.moveToFirst();
String dateAppointment=null;
String physicName=null;
System.out.println(" Cursor count "+cursor.getCount());
for(int i=0;i<cursor.getCount();i++)
{
dateAppointment =cursor.getString((cursor.getColumnIndex("DateofAppointment")));
physicName =cursor.getString((cursor.getColumnIndex("PhysicianName")));
dateofAppointment[i]=dateAppointment;
physicianName[i]=physicName;
System.out.println("Physicain name "+ physicianName[i]+ " "+i);
cursor.moveToNext();
}
Appointment apt=new Appointment();
apt.dateofAppointment=dateofAppointment;
apt.physicianName=physicianName;
apt.fillCountryTable();
System.out.println("in getAppointment---4");
//startManagingCursor(cursor);
} catch (Exception e)
{
System.out.println("Excetion in getAppointment"+e.getMessage());
}
int numberOfRows = cursor.getCount();
System.out.println("numberOfRows::"+numberOfRows);
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match.Register First.\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
/*public static void setDB(DBHelper1 dB2) {
DB1=dB2;
}*/
//display current date
public void setCurrentDateOnView() {
//dpResult =(DatePicker)findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
/*.append(year).append("-").append(month + 1).append("-")
.append(day).append(" "));*/
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
//dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
/*tvDisplayDate.setText(new StringBuilder().append(year)
.append("-").append(month + 1).append("-").append(day)
.append(" "));*/
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
//dpResult.init(year, month, day, null);
}
/*@Override
public void onDateSet(android.widget.DatePicker arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}*/
};
}
appointment1.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="This page helps you to take appointments"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text="HomePage" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="AddAppointment" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="wrap_content" android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffccd0"
>
<TextView
android:text="Appointments"
android:textColor="#b3000d"
android:gravity="center_vertical|center_horizontal"
android:textSize="26dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:background="#ffb0b6"
android:layout_marginBottom="5dip"
android:typeface="sans"/>
<RelativeLayout android:id="@+id/rl_country_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#555555">
<TextView android:id="@+id/tv_11"
android:layout_width="160dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="PhysicianName"
android:textStyle="normal|bold"
android:textColor="#FFFFFF"
android:textSize="18dip">
</TextView>
<TextView android:id="@+id/tv_12"
android:layout_width="170dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="DateOfAppointment"
android:textStyle="normal|bold"
android:textColor="#FFFFFF"
android:textSize="18dip"
android:layout_toRightOf="@+id/tv_11">
</TextView>
</RelativeLayout>
<!-- <LinearLayout android:id="@+id/ll_country"
android:layout_height="fill_parent" android:layout_width="fill_parent"> -->
<ScrollView android:id="@+id/ScrollView11"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<!-- <LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_margin="5dip"> -->
<TableLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:stretchColumns="*" android:id="@+id/appointment_table">
</TableLayout>
<!-- </LinearLayout> -->
</ScrollView>
<!-- </LinearLayout> -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
logcat的
07-10 23:58:58.850: E/AndroidRuntime(1221): FATAL EXCEPTION: main
07-10 23:58:58.850: E/AndroidRuntime(1221): Process: com.example.app, PID: 1221
07-10 23:58:58.850: E/AndroidRuntime(1221): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Appointment1}: java.lang.NullPointerException
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.os.Looper.loop(Looper.java:136)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-10 23:58:58.850: E/AndroidRuntime(1221): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 23:58:58.850: E/AndroidRuntime(1221): at java.lang.reflect.Method.invoke(Method.java:515)
07-10 23:58:58.850: E/AndroidRuntime(1221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-10 23:58:58.850: E/AndroidRuntime(1221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-10 23:58:58.850: E/AndroidRuntime(1221): at dalvik.system.NativeStart.main(Native Method)
07-10 23:58:58.850: E/AndroidRuntime(1221): Caused by: java.lang.NullPointerException
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
07-10 23:58:58.850: E/AndroidRuntime(1221): at com.example.app.AddAppointment.getAppointment(AddAppointment.java:196)
07-10 23:58:58.850: E/AndroidRuntime(1221): at com.example.app.Appointment1.onCreate(Appointment1.java:39)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.Activity.performCreate(Activity.java:5231)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-10 23:58:58.850: E/AndroidRuntime(1221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-10 23:58:58.850: E/AndroidRuntime(1221): ... 11 more
答案 0 :(得分:1)
你有
public class AddAppointment extends ActionBarActivity
此
AddAppointment obj=new AddAppointment();
obj.getAppointment();
是问题所在。您不应该实例化Activity类将导致null上下文。
你应该重新考虑你的设计。您可以创建一个非活动类(实用程序类),然后创建一个方法来完成工作并在Activity类中调用该方法。但是你无法实例化一个Activity类。