0

Getting a constant crash on the emulator when running a specific activity in my android project. The main activity of the project, as well as the splash screen run perfectly. The activity I am trying to run is quite complex, with information being read from a phpMyAdmin database.

I have read through the LogCat, but cannot seem to pinpoint the error that is causing the crash. Any nods in the right direction would be much appreciated. Don't have too much experience with this type of stuff.

Many thanks and apologies for the messy LogCat.

Maintenance 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_maintenance

    <?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)

Adapter Class

    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);

        }
    }
}
Kali Ma
  • 125
  • 2
  • 16

1 Answers1

1

You should migrate to 26.0.0 or above version for your support v7 library.

This is issue in appcompat library and mentioned here

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

ResourcesCompat.getFont() throws exception

You should use the same version across all Android-related dependencies,its a best practice

You can check appcompat dependencies using

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

It will give you info from where appcompat version is bumped to 27.0.2 from 26.1.0

Akhil
  • 6,667
  • 4
  • 31
  • 61
  • Is it just a case of changing it in the build.gradle? I'm not sure the steps for doing this correctly. – Kali Ma Feb 06 '18 at 17:59
  • It says that all Android libraries must be on the same version specification, and saying that I have one on 27.0.2. The ones that I can see are all on 26.1.0. Is there a way that I can see all Android libraries and their versions? – Kali Ma Feb 06 '18 at 18:08
  • edited my answer – Akhil Feb 07 '18 at 06:05