|
|
|
[Android Studio] ถามเรื่อง Error นี้หน่อยครับ หรือพอมีวิธีสั่งปิดหน้า layout หน่อยครับ |
|
|
|
|
|
|
|
นี้อะครับ error เท่าที่สังเกตอะครับ ถ้าผมใช้คำสั่ง finish(); ก่อนที่จะให้มันเปลี่ยนหน้ามันจะ error อะครับ (ปุ่ม upload) แต่ถ้าใช้คำสั่งนี้ก็รันได้ปกติครับ บรรทัดที่ 154 ครับ
Code (Android-Java)
package com.example.namwanta.midtermproject;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;
public class Addpromotion extends AppCompatActivity implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
private Button btHome, btLogout, btMenu, btProfile;
private Button buttonChoose;
private Button buttonUpload;
private Button btnStart;
private Button btnEnd;
private ImageView imageView;
private EditText editTextName;
private EditText editTextDetail;
private EditText editTextStart;
private EditText editTextEnd;
private RadioButton rCustomer;
private RadioButton rOwner;
private Spinner spin1;
private String memberID = "";
private String StatusMember = "";
private Double Latitude;
private Double Longitude;
private int mYear;
private int mMonth;
private int mDay;
private int i = 0;
static final int DATE_DIALOG_ID = 0;
private Bitmap bitmap;
private int PICK_IMAGE_REQUEST = 1;
private String UPLOAD_URL = "http://202.28.94.32/Photoupload/upload2.php";
private String KEY_IMAGE = "image";
private String KEY_NAME = "name";
private String KEY_DETAIL = "detail";
private String KEY_START = "start";
private String KEY_END = "end";
private String KEY_Status = "status";
private String KEY_Type = "type";
private String KEY_Lat = "latitude";
private String KEY_Long = "longitude";
private String KEY_MemberID = "memberid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpromotion);
spin1 = (Spinner) findViewById(R.id.spinner1);
btHome = (Button) findViewById(R.id.btHome);
btLogout = (Button) findViewById(R.id.btLogout);
btMenu = (Button) findViewById(R.id.btMenu);
btProfile = (Button) findViewById(R.id.btProfile);
buttonChoose = (Button) findViewById(R.id.btChoose);
buttonUpload = (Button) findViewById(R.id.btAddpro);
btnStart = (Button) findViewById(R.id.btnStart);
btnEnd = (Button) findViewById(R.id.btnEnd);
editTextName = (EditText) findViewById(R.id.etName);
editTextDetail = (EditText) findViewById(R.id.etDetail);
editTextStart = (EditText) findViewById(R.id.etStart);
editTextEnd = (EditText) findViewById(R.id.etEnd);
rCustomer = (RadioButton) findViewById(R.id.radioCustomer);
rOwner = (RadioButton) findViewById(R.id.radioOwner);
imageView = (ImageView) findViewById(R.id.imageView);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
//Get Session Login
final UserHelper usrHelper = new UserHelper(this);
//Get Login Status
if (!usrHelper.getLoginStatus()) {
Intent newActivity = new Intent(Addpromotion.this, Login.class);
startActivity(newActivity);
}
//Get member id from session
memberID = usrHelper.getMemberID();
//เงื่อนไขเมื่อกดปุ่มให้ทำอะไร
buttonChoose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showFileChooser();
}
});
buttonUpload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
upload();
if (rCustomer.isChecked()) {
StatusMember = "Customer";
} else if (rOwner.isChecked()) {
StatusMember = "ShopOwner";
}
Intent main = new Intent(Addpromotion.this, MainActivity.class);
startActivity(main);
finish();
}
});
btHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent main = new Intent(Addpromotion.this, MainActivity.class);
startActivity(main);
finish();
}
});
btLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
btMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent main = new Intent(Addpromotion.this, Menu.class);
startActivity(main);
finish();
}
});
btProfile.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent main = new Intent(Addpromotion.this, Profile.class);
startActivity(main);
finish();
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
i = 1;
}
});
btnEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
i = 2;
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateCurrentDate();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
// Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
// Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Log.d("Latitude","status");
}
//เปิดหน้าปัดเวลา
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
// อัพเดทเวลาลงใน edittext
private void updateCurrentDate() {
if (i == 0) {
editTextStart.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).append(""));
editTextEnd.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).append(""));
}
if (i == 1) {
editTextStart.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).append(""));
}
if (i == 2) {
editTextEnd.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay).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;
updateCurrentDate();
}
};
//บีบรูป
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
//เซฟไฟล์ไปmysql
private void upload() {
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(Addpromotion.this, s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(Addpromotion.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
})
//ส่งค่าไปphp
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = editTextName.getText().toString().trim();
String detail = editTextDetail.getText().toString().trim();
String start = editTextStart.getText().toString().trim();
String end = editTextEnd.getText().toString().trim();
String status = StatusMember;
String type = String.valueOf(spin1.getSelectedItem());
String latitude = Latitude.toString();
String longitude = Longitude.toString();
String memberid = memberID;
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, name);
params.put(KEY_DETAIL, detail);
params.put(KEY_START, start);
params.put(KEY_END, end);
params.put(KEY_Status, status);
params.put(KEY_Type, type);
params.put(KEY_Lat, latitude);
params.put(KEY_Long, longitude);
params.put(KEY_MemberID, memberid);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
//โชว์รูปในอัลบั้ม
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Tag : Mobile, Android, Mobile
|
ประวัติการแก้ไข 2016-03-06 00:31:29
|
|
|
|
|
Date :
2016-03-06 00:30:08 |
By :
nomzod |
View :
2424 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหมือนจะมีการเปลี่ยนหน้าแล้ว ตัว Process มันหา Widget ที่อ้างถึงไม่เจอน่ะครับ
|
|
|
|
|
Date :
2016-03-06 10:58:51 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีวิธีแก้ไหมครับ หรือมีวิธีตั้งเวลาให้มันปิดหน้าช้ากว่านี้หน่อยไหมครับ
|
|
|
|
|
Date :
2016-03-06 12:50:27 |
By :
nomzod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gfdsgsdgsdfgdfs
|
|
|
|
|
Date :
2020-02-21 16:15:10 |
By :
gdsfg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|