我在Createprofile类中创建了一个对象,并将ID值传递给我的CreateProfile1类:
我的CreateProfile类中的代码:
public class CreateProfile extends Activity {
TextView tv;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
ImageView imgview;
int z=0;
byte b[];
ImageView img;
EditText Idno = null;
EditText GIDno=null;
EditText Name= null;
EditText Owner= null;
EditText Date=null;
AutoCompleteTextView Breed = null;
private DBAdapter DBHelper = null;
private CreateProfile1 PF = null;
String name, ID,GID,ow,br,date;
private EditText mDateDisplay;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 1;
String mm="";
public void auto(){
// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView txtView = (AutoCompleteTextView) findViewById(R.id.autocomplete_breed);
// Get the string array
String[] breed = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, breed);
txtView.setThreshold(1);
txtView.setAdapter(adapter);
}
public int radiox(){
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
int a=0;
switch (checkedRadioButton) {
case R.id.radiobutton3 :
a = 1;
break;
case R.id.radiobutton4 :
a = 2;
break;
case R.id.radiobutton5 :
a = 3;
break;
}
return a;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
DBHelper = new DBAdapter(this);
PF = new CreateProfile1();
//MediaPlayer MyMp = MediaPlayer.create(this, R.raw.robo);
//MyMp.start();
String fontPath = "fonts/COPRGTB.TTF";
tv=(TextView)findViewById(R.id.textView1);
TextView tv1=(TextView)findViewById(R.id.textView2);
TextView tv2=(TextView)findViewById(R.id.textView3);
TextView tv3=(TextView)findViewById(R.id.textView8);
TextView tv4=(TextView)findViewById(R.id.textView4);
TextView tv5=(TextView)findViewById(R.id.textView5);
TextView tv6=(TextView)findViewById(R.id.textView6);
TextView tv8=(TextView)findViewById(R.id.textView9);
TextView tv9=(TextView)findViewById(R.id.textView10);
Button button=(Button)findViewById(R.id.btn2);
//button.setOnClickListener(sav);
RadioButton r1=(RadioButton)findViewById(R.id.radiobutton3);
RadioButton r2=(RadioButton)findViewById(R.id.radiobutton4);
RadioButton r3=(RadioButton)findViewById(R.id.radiobutton5);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
tv.setTypeface(tf);
tv1.setTypeface(tf);
tv2.setTypeface(tf);
tv3.setTypeface(tf);
tv4.setTypeface(tf);
tv5.setTypeface(tf);
tv6.setTypeface(tf);
tv8.setTypeface(tf);
tv9.setTypeface(tf);
button.setTypeface(tf);
r1.setTypeface(tf);
r2.setTypeface(tf);
r3.setTypeface(tf);
auto();
Idno = (EditText)findViewById(R.id.editText1);
GIDno = (EditText)findViewById(R.id.editTextx);
Name = (EditText)findViewById(R.id.editText2);
Owner = (EditText)findViewById(R.id.editText4);
Breed = (AutoCompleteTextView)findViewById(R.id.autocomplete_breed);
Date = (EditText)findViewById(R.id.editText3);
Button Save = (Button)findViewById(R.id.btn2);
Save.setOnClickListener(onSave);
// Button check=(Button)findViewById(R.id.btnx);
// check.setOnClickListener(oncheck);
Button pic=(Button)findViewById(R.id.btn1);
imgview = (ImageView)this.findViewById(R.id.imageView1);
pic.setOnClickListener(picc);
//for datepicker
mDateDisplay = (EditText) findViewById(R.id.editText3);
Button pickDate = (Button) findViewById(R.id.btn3);
pickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
//for date picker
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog)
{
switch (id)
{
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay()
{
int month=mMonth+1;
if(month==1)
{
mm="Jan";
}
else if(month==2)
{
mm="Feb";
}
else if(month==3)
{
mm="Mar";
}
else if(month==4)
{
mm="Apr";
}
else if(month==5)
{
mm="May";
}
else if(month==6)
{
mm="Jun";
}
else if(month==7)
{
mm="Jul";
}
else if(month==8)
{
mm="Aug";
}
else if(month==9)
{
mm="Sep";
}
else if(month==10)
{
mm="Oct";
}
else if(month==11)
{
mm="Nov";
}
else if(month==12)
{
mm="Dec";
}
mDateDisplay.setText
(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
// .append(mMonth + 1).append("-")
.append(mm).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
//for camera
private View.OnClickListener picc=new View.OnClickListener()
{
public void onClick(View v)
{
openNewGameDialog();
}
};
private void openNewGameDialog()
{
new AlertDialog.Builder(this).setItems(R.array.Menu2,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface,int i)
{
if(i==0)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 110);
intent.putExtra("outputY", 60);
try
{
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
catch (ActivityNotFoundException e)
{
// Do nothing for now
}
// Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(cameraIntent, CAMERA_REQUEST);
//Intent CreateProfile = new Intent(MenuScreen.this,CreateProfile.class);
//startActivity(CreateProfile);
}
else if (i==1)
{
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 110);
intent.putExtra("outputY", 60);
try
{
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
}
catch (ActivityNotFoundException e)
{
// Do nothing for now
}
// Intent CheckProfile = new Intent(MenuScreen.this,CreateProfile.class);
// startActivity(CheckProfile);
// Intent gal = new Intent(
// Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(gal, RESULT_LOAD_IMAGE);
}
}
})
.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_FROM_CAMERA)
{
Bundle extras = data.getExtras();
if (extras != null)
{
Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(photo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, baos); //photo is the bitmap object
b = baos.toByteArray();
}
}
if (requestCode == PICK_FROM_GALLERY)
{
Bundle extras2 = data.getExtras();
if (extras2 != null)
{
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, baos); //photo is the bitmap object
b = baos.toByteArray();
}
}
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v){
DBHelper.open();
boolean y =false;
y = fillData();
z = radiox();
br= Breed.getText().toString();
ow= Owner.getText().toString();
GID = GIDno.getText().toString();
boolean k = false;
boolean r = false;
boolean d = false;
boolean o=false;
boolean p=false;
o=datevalidation();
if (o){
Date.setError("Your Horse can't be born in the future");
p=true;
}
else{
date= Date.getText().toString();
}
if (z==0){
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_LONG).show();
k=true;
}
if(Name.getText().toString().length() < 2 ){
Name.setError( "Enter Name" );
r=true;
}
else{
name = Name.getText().toString();
}
if(y==true || Idno.getText().toString().length()<1){
Idno.setError("Enter a Unique ID");
d=true;
}
else{
ID= Idno.getText().toString();
}
if(k==false && r == false && d == false && p ==false){
DBHelper.insertD(ID, GID, name, ow, br,z,date, b);
Toast.makeText(getApplicationContext(), "Database Created And Data Saved", Toast.LENGTH_LONG).show();
Intent profiletwo = new Intent(CreateProfile.this,CreateProfile1.class);
startActivity(profiletwo);
DBHelper.close();
PF.getid(ID);
}
else if(k==true || r==true || d ==true || p == true){
Toast.makeText(getApplicationContext(), "Please check if form Data is entered correctly", Toast.LENGTH_LONG).show();
}
}
};
private boolean fillData() {
Cursor c = DBHelper.fetchAllIDs();
boolean x = false;
// List<String> idList = new ArrayList<String>();
if (c.moveToFirst()) {
do {
String X = (c.getString(c.getColumnIndexOrThrow("IDno")));
String B = null;
B = Idno.getText().toString();
if (B.equals(X)){
x=true;
}
} while (c.moveToNext());
}
return x;
}
public boolean datevalidation(){
boolean m= false;
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
int a = today.monthDay;
int b = (today.month+1);
int c = today.year;
if (mYear > c){
m=true;
}
if (mYear==c && (mMonth+1)>b){
m=true;
}
if(mYear==c && (mMonth+1) == b && mDay >a){
m=true;
}
return m;
}
}
我的CreateProfile1类是这样的:
public class CreateProfile1 extends Activity {
Context context;
String Identity= null;
public int VID;
public int DateDay;
public int DateMonth;
int DateYear;
int hours,minutes;
public void getid(String ID){
Identity = ID;
}
public void getVaccination(int VacciID){
VID = VacciID;
Toast.makeText(getApplicationContext(), ""+VID, Toast.LENGTH_LONG).show();
}
public void getDate(int mYear, int mMonth, int mDay){
DateYear = mYear;
DateMonth = mMonth;
DateDay = mDay;
Toast.makeText(getApplicationContext(), ""+DateYear+DateMonth+DateDay, Toast.LENGTH_LONG).show();
}
public void getTime(int Hours, int Minutes){
hours = Hours;
minutes=Minutes;
Toast.makeText(getApplicationContext(), ""+hours+minutes, Toast.LENGTH_LONG).show();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile1);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
String fontPath = "fonts/COPRGTB.TTF";
TextView tv1=(TextView)findViewById(R.id.textView1);
TextView tv2=(TextView)findViewById(R.id.textView2);
TextView tv3=(TextView)findViewById(R.id.textView3);
TextView tv4=(TextView)findViewById(R.id.textView4);
TextView tv5=(TextView)findViewById(R.id.textView5);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
Button button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(vaccination);
Button button4=(Button)findViewById(R.id.button4);
Button button5=(Button)findViewById(R.id.button5);
Button button6=(Button)findViewById(R.id.button6);
Button button7=(Button)findViewById(R.id.button7);
button1.setOnClickListener(onButton1);
button2.setOnClickListener(deworming);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
// Applying font
tv1.setTypeface(tf);
tv2.setTypeface(tf);
tv3.setTypeface(tf);
tv4.setTypeface(tf);
tv5.setTypeface(tf);
button1.setTypeface(tf);
button2.setTypeface(tf);
button3.setTypeface(tf);
button4.setTypeface(tf);
button5.setTypeface(tf);
button6.setTypeface(tf);
button7.setTypeface(tf);
}
private View.OnClickListener onButton1=new View.OnClickListener() {
public void onClick(View v){
File pdfFile = new File("/sdcard/" +"BCS.pdf");
if(pdfFile.exists()) {
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e){
Toast.makeText(CreateProfile1.this, "File does not exist", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), ""+VID, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), ""+Identity, Toast.LENGTH_LONG).show();
}
}
}
};
private View.OnClickListener vaccination=new View.OnClickListener()
{
public void onClick(View v)
{
Intent vaccin = new Intent(CreateProfile1.this,Vaccination.class);
startActivity(vaccin);
}
};
private View.OnClickListener deworming=new View.OnClickListener(){
public void onClick(View v){
Intent Deworm = new Intent(CreateProfile1.this,DeWorming.class);
startActivity(Deworm);
}
};
日志Cat显示:
10-08 14:12:56.603: D/dalvikvm(329): GC_EXTERNAL_ALLOC freed 51K, 53% free 2563K/5379K, external 1625K/2137K, paused 42ms
10-08 14:12:57.454: E/tag(329): fonts
10-08 14:12:57.454: E/tag(329): images
10-08 14:12:57.454: E/tag(329): sounds
10-08 14:12:57.454: E/tag(329): webkit
10-08 14:13:02.813: D/dalvikvm(329): GC_EXTERNAL_ALLOC freed 16K, 52% free 2584K/5379K, external 3502K/3903K, paused 41ms
10-08 14:13:34.544: W/KeyCharacterMap(329): No keyboard for id 0
10-08 14:13:34.544: W/KeyCharacterMap(329): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-08 14:13:44.894: D/AndroidRuntime(329): Shutting down VM
10-08 14:13:44.894: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-08 14:13:44.903: E/AndroidRuntime(329): FATAL EXCEPTION: main
10-08 14:13:44.903: E/AndroidRuntime(329): java.lang.NullPointerException
10-08 14:13:44.903: E/AndroidRuntime(329): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
10-08 14:13:44.903: E/AndroidRuntime(329): at com.gallops.CreateProfile1.getid(CreateProfile1.java:24)
10-08 14:13:44.903: E/AndroidRuntime(329): at com.gallops.CreateProfile$3.onClick(CreateProfile.java:446)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.view.View.performClick(View.java:2485)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.view.View$PerformClick.run(View.java:9080)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.os.Handler.handleCallback(Handler.java:587)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123)
10-08 14:13:44.903: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-08 14:13:44.903: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:13:44.903: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 14:13:44.903: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-08 14:13:44.903: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-08 14:13:44.903: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method)
10-08 14:18:45.614: D/dalvikvm(344): GC_EXTERNAL_ALLOC freed 57K, 53% free 2567K/5379K, external 2002K/2137K, paused 41ms
使用后的Log Cat显示:
Toast.makeText(CreateProfile1.this, ""+Identity, Toast.LENGTH_LONG).show();
10-08 14:30:20.313: D/dalvikvm(329): GC_EXTERNAL_ALLOC freed 51K, 53% free 2563K/5379K, external 1625K/2137K, paused 41ms
10-08 14:30:21.052: E/tag(329): fonts
10-08 14:30:21.052: E/tag(329): images
10-08 14:30:21.052: E/tag(329): sounds
10-08 14:30:21.052: E/tag(329): webkit
10-08 14:30:24.072: D/dalvikvm(329): GC_EXTERNAL_ALLOC freed 17K, 52% free 2586K/5379K, external 3502K/3903K, paused 34ms
10-08 14:30:31.993: W/KeyCharacterMap(329): No keyboard for id 0
10-08 14:30:31.993: W/KeyCharacterMap(329): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-08 14:30:42.053: D/AndroidRuntime(329): Shutting down VM
10-08 14:30:42.053: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-08 14:30:42.062: E/AndroidRuntime(329): FATAL EXCEPTION: main
10-08 14:30:42.062: E/AndroidRuntime(329): java.lang.NullPointerException
10-08 14:30:42.062: E/AndroidRuntime(329): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.widget.Toast.<init>(Toast.java:89)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.widget.Toast.makeText(Toast.java:231)
10-08 14:30:42.062: E/AndroidRuntime(329): at com.gallops.CreateProfile1.getid(CreateProfile1.java:26)
10-08 14:30:42.062: E/AndroidRuntime(329): at com.gallops.CreateProfile$3.onClick(CreateProfile.java:446)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.view.View.performClick(View.java:2485)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.view.View$PerformClick.run(View.java:9080)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.os.Handler.handleCallback(Handler.java:587)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123)
10-08 14:30:42.062: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-08 14:30:42.062: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:30:42.062: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:507)
10-08 14:30:42.062: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-08 14:30:42.062: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-08 14:30:42.062: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
不使用 ApplicationContext ,而是使用您的Activity的上下文。将 getApplicationContext ()替换为您的活动上下文,
Toast.makeText(ActivityName.this, ""+Identity, Toast.LENGTH_LONG).show();
要了解这些背景read this.
之间的区别