我正在android studio 3.3.2中开发一个android应用程序(条形码扫描仪)。我想将其与远程数据库链接,该应用程序可以构建并成功运行,但是当我扫描条形码时,它崩溃了,并返回了一个空的活动。
我尝试调试错误,但无济于事,我是android studio的新手。我试图用凌空抽烟。
Java 打包com.example.scan;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://192.168.43.232/Product/Api.php";
public static final String MyPREFERENCES = "MyPrefs";
RecyclerView recyclerView;
ArrayList<ListItem> arrayList;
MyAdapter myAdapter;
CoordinatorLayout coordinatorLayout;
RelativeLayout relativeLayout;
List<ListItem> listItemsArrayList;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.ralativeLayout);
recyclerView = (RecyclerView) findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setNestedScrollingEnabled(false);
int position = 1;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
loadProducts();
listItemsArrayList = new ArrayList<>();
/* if (listItemsArrayList.size() > 0) {
myAdapter = new MyAdapter(listItemsArrayList, this);
recyclerView.setAdapter(myAdapter);
} else {
Toast.makeText(getApplicationContext(), "No data found", Toast.LENGTH_LONG).show();
}
*/
final IntentIntegrator intentIntegrator = new IntentIntegrator(this);
intentIntegrator.setBeepEnabled(true);
intentIntegrator.setCameraId(0);
final int finalPosition = position;
FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intentIntegrator.initiateScan();
}
});
}
private void loadProducts() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject product = array.getJSONObject(i);
//adding the product to product list
listItemsArrayList.add(new ListItem(
product.getString("product_name"),
product.getDouble("price")
));
}
//creating adapter object and setting it to recyclerview
MyAdapter adapter = new MyAdapter(listItemsArrayList, MainActivity.this);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
@Override
public void onBackPressed() {
showAlertDialog();
}
private void showAlertDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Exit");
builder.setMessage("Are you sure you want to leave? ");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(getApplicationContext(), "No result found", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
arrayList.clear();
myAdapter = new MyAdapter(arrayList, this);
recyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
MyAdapter代码
Java 打包com.example.scan;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterViewHolder> {
List<ListItem> listItemsArrayList;
Context context;
public MyAdapter(List<ListItem> listItemsArrayList, Context context) {
this.listItemsArrayList = listItemsArrayList;
this.context = context;
}
@NonNull
@Override
public MyAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout, parent, false);
return new MyAdapterViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyAdapterViewHolder holder, int position) {
ListItem listItem = listItemsArrayList.get(position);
holder.textViewCode.setText(listItem.getProduct_name());
holder.textViewType.setText(String.valueOf(listItem.getPrice()));
Linkify.addLinks(holder.textViewCode, Linkify.ALL);
}
@Override
public int getItemCount() {
return listItemsArrayList.size();
}
public void removeItem(int position) {
listItemsArrayList.remove(position);
// notify the item removed by position
// to perform recycler view delete animations
// NOTE: don't call notifyDataSetChanged()
notifyItemRemoved(position);
}
public void restoreItem(ListItem listItem, int position) {
listItemsArrayList.add(position, listItem);
// notify item added by position
notifyItemInserted(position);
}
public class MyAdapterViewHolder extends RecyclerView.ViewHolder {
TextView textViewCode, textViewType;
CardView cardView;
public MyAdapterViewHolder(final View itemView) {
super(itemView);
textViewCode = (TextView) itemView.findViewById(R.id.textViewCode);
textViewType = (TextView) itemView.findViewById(R.id.textViewType);
cardView = (CardView) itemView.findViewById(R.id.cardView);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String type = listItemsArrayList.get(getAdapterPosition()).getProduct_name();
Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, type);
i.setType("text/plain");
itemView.getContext().startActivity(i);
}
});
}
}
}
我希望XAMPP数据库的product_name和price的输出将显示在文本视图上。