Java Android Studio中的更新数据错误,但API有效
我试图更新数据,API在Postman中运行,但当我在Android Studio中运行应用程序时,出现消息“不幸的是,driverApplication已停止”。有人知道解决办法吗?多谢各位 错误:Java Android Studio中的更新数据错误,但API有效,java,android,api,android-studio,Java,Android,Api,Android Studio,我试图更新数据,API在Postman中运行,但当我在Android Studio中运行应用程序时,出现消息“不幸的是,driverApplication已停止”。有人知道解决办法吗?多谢各位 错误: W/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overr
W/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
E/RecyclerView: No adapter attached; skipping layout
I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
V/RenderScript: 0xbe134c00 Launching thread(s), CPUs 2
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.driverapplication, PID: 5607
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.driverapplication.model.changeStatus.ChangeStatus.getMessage()' on a null object reference
at com.example.driverapplication.activity.driver.ChangeStatusActivityDriver$2.onResponse(ChangeStatusActivityDriver.java:105)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5307)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 5607 SIG: 9
API接口:
@FormUrlEncoded
@POST("changeStatus")
Call<ChangeStatus> changeStatusResponse(
@Field("id_order") String id_order,
@Field("id_trip") String id_trip,
@Field("id_seat") String id_seat,
@Field("status") Integer status
);
模型(ChangeStatusData.java)
活动(ChangeStatusActivityDriver.java):
公共类ChangeStatusActivityDriver扩展了AppCompatActivity{
公共静态最终字符串EXTRA\u CHANGE\u STATUS\u DRIVER=“EXTRA\u CHANGE\u STATUS\u DRIVER”;
文本视图tvName;
纺纱机;纺纱机状态;
按钮更新;
字符串名称、选定项、idOrder、idTrip、idSeat、状态字符串;
整数状态;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u change\u status\u driver);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(“按驱动程序编辑状态”);
DetailTripDriverData DetailTripDriverData=getIntent().getParcelableExtra(额外更改状态驱动程序);
Name=detailTripDriverData.getPassengerName();
statusString=detailTripDriverData.getStatus();
idOrder=detailTripDriverData.getIdOrder();
idTrip=detailTripDriverData.getIdTrip();
idSeat=detailTripDriverData.getIdSeat();
tvName=findviewbyd(R.id.tv\u name\u驱动程序);
tvName.setText(名称);
spinnerStatus=findViewById(R.id.spinner\u status\u driver);
btnUpdate=findviewbyd(R.id.btn\u更新\u驱动程序);
btnUpdate.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
选定=喷丝头状态。getSelectedItem().toString();
开关(选定){
案例“预订”:
状态=1;
打破
“拾起”案例:
状态=2;
打破
“进行中”案例:
状态=3;
打破
“抵达”一案:
状态=4;
打破
“已取消”案例:
状态=5;
打破
}
Toast.makeText(ChangeStatusActivityDriver.this,“Status=“+Selected+”| Code”+状态,Toast.LENGTH_SHORT).show();
changeStatus();
}
});
}
@凌驾
公共布尔onSupportNavigateUp(){
onBackPressed();
返回true;
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
}
私有无效更改状态(){
ApiInterface ApiInterface=ApiClient.getClient().create(ApiInterface.class);
Call changeStatusCall=apiInterface.changeStatusResponse(idOrder、idTrip、idSeat、Status);
changeStatusCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null&&response.issusccessful()&&response.body().isStatus()){
Toast.makeText(ChangeStatusActivityDriver.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
Intent intentBackDriver=new Intent(ChangeStatusActivityDriver.this,DetailTripDriver.class);
启动触觉(intentBackDriver);
完成();
}否则{
Toast.makeText(ChangeStatusActivityDriver.this,response.body().getMessage(),Toast.LENGTH_SHORT).show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(ChangeStatusActivityDriver.this,t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
布局(activitychangestatus_driver.xml)
您试图在正文为空时获取消息。在访问之前添加空检查
if (response.body() != null) {
Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(),Toast.LENGTH_SHORT).show();
}
if(response.body()!=null…{…}else{…response.body().getMessage()…}
你真的在问为什么?
public class ChangeStatusData {
@SerializedName("id_seat")
private String idSeat;
@SerializedName("id_trip")
private String idTrip;
@SerializedName("id_order")
private String idOrder;
@SerializedName("status")
private Integer status;
public void setIdSeat(String idSeat){
this.idSeat = idSeat;
}
public String getIdSeat(){
return idSeat;
}
public void setIdTrip(String idTrip){
this.idTrip = idTrip;
}
public String getIdTrip(){
return idTrip;
}
public void setStatus(Integer status){
this.status = status;
}
public Integer getStatus(){
return status;
}
}
public class ChangeStatusActivityDriver extends AppCompatActivity {
public static final String EXTRA_CHANGE_STATUS_DRIVER = "extra_change_status_driver";
TextView tvName;
Spinner spinnerStatus;
Button btnUpdate;
String Name, Selected, idOrder, idTrip, idSeat, statusString;
Integer Status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_status_driver);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Edit Status by Driver");
DetailTripDriverData detailTripDriverData = getIntent().getParcelableExtra(EXTRA_CHANGE_STATUS_DRIVER);
Name = detailTripDriverData.getPassengerName();
statusString = detailTripDriverData.getStatus();
idOrder = detailTripDriverData.getIdOrder();
idTrip = detailTripDriverData.getIdTrip();
idSeat = detailTripDriverData.getIdSeat();
tvName = findViewById(R.id.tv_name_driver);
tvName.setText(Name);
spinnerStatus = findViewById(R.id.spinner_status_driver);
btnUpdate = findViewById(R.id.btn_update_driver);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Selected = spinnerStatus.getSelectedItem().toString();
switch (Selected){
case "Booking":
Status = 1;
break;
case "Picked Up":
Status = 2;
break;
case "On Going":
Status = 3;
break;
case "Arrived":
Status = 4;
break;
case "Cancelled":
Status = 5;
break;
}
Toast.makeText(ChangeStatusActivityDriver.this, "Status = " +Selected+ " | Code " +Status, Toast.LENGTH_SHORT).show();
changeStatus();
}
});
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
private void changeStatus() {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<ChangeStatus> changeStatusCall = apiInterface.changeStatusResponse(idOrder, idTrip, idSeat, Status);
changeStatusCall.enqueue(new Callback<ChangeStatus>() {
@Override
public void onResponse(Call<ChangeStatus> call, Response<ChangeStatus> response) {
if (response.body() != null && response.isSuccessful() && response.body().isStatus()){
Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
Intent intentBackDriver = new Intent(ChangeStatusActivityDriver.this, DetailTripDriver.class);
startActivity(intentBackDriver);
finish();
} else {
Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ChangeStatus> call, Throwable t) {
Toast.makeText(ChangeStatusActivityDriver.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.driver.ChangeStatusActivityDriver">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:layout_below="@+id/title">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/headings"
android:text="Name"
android:textSize="13sp"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name_driver"
android:fontFamily="sans-serif-light"
android:text="Passenger One"
android:textColor="#000000"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="@color/grey" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status"
android:textColor="@color/headings"
android:textSize="13sp"
android:layout_marginBottom="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:id="@+id/spinner_status_driver"
android:textSize="18sp"
android:entries="@array/array_status"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<Button
android:id="@+id/btn_update_driver"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/button_rounded"
android:fontFamily="sans-serif-light"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Update"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
if (response.body() != null) {
Toast.makeText(ChangeStatusActivityDriver.this, response.body().getMessage(),Toast.LENGTH_SHORT).show();
}