gpt4 book ai didi

java - 打开特定 Activity 时令人沮丧的崩溃

转载 作者:行者123 更新时间:2023-12-02 11:40:05 25 4
gpt4 key购买 nike

在我的 Android 项目中运行特定 Activity 时,模拟器上不断崩溃。该项目的主要 Activity 以及启动画面运行完美。我尝试运行的 Activity 非常复杂,信息是从 phpMyAdmin 数据库读取的。

我已通读 LogCat,但似乎无法查明导致崩溃的错误。任何朝正确方向的点头将不胜感激。对此类东西没有太多经验。

非常感谢并对困惑的 LogCat 表示歉意。

维护 Activity

  package com.example.bs.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

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 Maintenance extends AppCompatActivity {

private static final String PRODUCT_URL = "http://192.XXXXXXX/MyApi/api.php";

RecyclerView recyclerView;
ProductAdapter adapter;

List<Product> productList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintenance);

productList = new ArrayList<>();

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

loadProducts ();

}

private void loadProducts () {

StringRequest stringRequest = new StringRequest(Request.Method.GET, PRODUCT_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray products =new JSONArray(response);

for (int i = 0; i<products.length(); i++){

JSONObject productObject = products.getJSONObject(i);

int maintenance_id = productObject.getInt("maintenance_id");
String type = productObject.getString("type");
String property = productObject.getString("property");
String more_info = productObject.getString("more_info");
Double date = productObject.getDouble("date");

Product product = new Product(maintenance_id, type, property, more_info, date);
productList.add(product);

}

adapter = new ProductAdapter(Maintenance.this, productList);
recyclerView.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}

}
},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Toast.makeText(Maintenance.this, error.getMessage(), Toast.LENGTH_SHORT).show();

}
});

Volley.newRequestQueue(this).add(stringRequest);

}
}

Activity 维护

    <?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="com.example.benchalmers.myapplication.Maintenance">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

LogCat

    02-06 12:11:42.195 4155-4155/? I/zygote: Not late-enabling -Xcheck:jni (already on)
02-06 12:11:42.409 4155-4155/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
02-06 12:11:43.494 4155-4155/com.example.bs.myapplication I/InstantRun: starting instant run server: is main process
02-06 12:11:43.665 4155-4155/com.example.bs.myapplication D/AndroidRuntime: Shutting down VM
02-06 12:11:43.671 4155-4155/com.example.bs.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bs.myapplication, PID: 4155
java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)Landroid/graphics/Typeface; in class Landroid/support/v4/content/res/ResourcesCompat; or its super classes (declaration of 'android.support.v4.content.res.ResourcesCompat' appears in /data/app/com.example.bs.myapplication-EpCz2N67lH5I_Aiqfiskfg==/split_lib_dependencies_apk.apk)
at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)
at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:110)
at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:81)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:67)
at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:753)
at android.support.v7.widget.ToolbarWidgetWrapper.setTitleInt(ToolbarWidgetWrapper.java:261)
at android.support.v7.widget.ToolbarWidgetWrapper.setWindowTitle(ToolbarWidgetWrapper.java:243)
at android.support.v7.widget.ActionBarOverlayLayout.setWindowTitle(ActionBarOverlayLayout.java:621)
at android.support.v7.app.AppCompatDelegateImplV9.onTitleChanged(AppCompatDelegateImplV9.java:631)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.bs.myapplication.Maintenance.onCreate(Maintenance.java:34)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

适配器类

    package com.example.benchalmers.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {


private Context mCtx;
private List<Product> productList;

public ProductAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new ProductViewHolder(view);

}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {

Product product = productList.get(position);

holder.textViewType.setText(product.getType());
holder.textViewProperty.setText(product.getProperty());
holder.textViewMoreInfo.setText(String.valueOf(product.getMoreInfo()));
holder.textViewDate.setText(String.valueOf(product.getDate()));

//holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
}

@Override
public int getItemCount() {
return productList.size();
}

class ProductViewHolder extends RecyclerView.ViewHolder {

ImageView imageView;
TextView textViewType, textViewProperty, textViewMoreInfo, textViewDate;

public ProductViewHolder(View itemView) {
super(itemView);

imageView = itemView.findViewById(R.id.imageView);
textViewType = itemView.findViewById(R.id.textViewType);
textViewProperty = itemView.findViewById(R.id.textViewProperty);
textViewMoreInfo = itemView.findViewById(R.id.textViewMoreInfo);
textViewDate = itemView.findViewById(R.id.textViewDate);

}
}
}

最佳答案

您应该迁移到 26.0.0 或更高版本的支持 v7 库。

这是 appcompat 库中的问题,此处提到

https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0

ResourcesCompat.getFont() throws exception

您应该在所有与 Android 相关的依赖项中使用相同的版本,这是最佳实践

您可以使用检查appcompat依赖项

./gradlew :app:dependencyInsight --dependency com.android.support:appcompat --configuration debugCompileClasspath

它将向您提供 appcompat 版本从 26.1.0 升级到 27.0.2 的信息

关于java - 打开特定 Activity 时令人沮丧的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com