Android Studio Beginner: Keep Getting NoSuchMethodException for android.graphics.FontFamily. <init> []

huangapple 未分类评论53阅读模式
英文:

Android Studio Beginner: Keep Getting NoSuchMethodException for android.graphics.FontFamily. <init> []

问题

这是MainActivity类:

package com.example.exploreroptimizer;

import android.app.ActivityManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Cleaner cleaner = new Cleaner();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button cleanButton = findViewById(R.id.cleanButton);
        final ProgressBar cleanBar = findViewById(R.id.progressBar);
        final TextView progressUpdate = findViewById(R.id.progressUpdate);
        System.out.println("success");

        cleanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            final ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
                            ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                            activityManager.getMemoryInfo(mi);
                            final double a = mi.availMem / 0x100000L;

                            cleanBar.setProgress(20);
                            progressUpdate.setText("Cleaning Ram!");
                            Thread.sleep(1500);
                            cleanBar.setProgress(40);

                            progressUpdate.setText("Closing Unused Apps!");
                            Thread.sleep(1500);
                            cleanBar.setProgress(100);

                            progressUpdate.setText("Clearing Cache!");
                            Thread.sleep(1500);
                            Cleaner.deleteCache(getApplicationContext());

                            cleaner.killBackgroundProcesses(getApplicationContext(), getApplicationContext().getPackageManager());
                            cleaner.clearRAM();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), "I have closed " + Cleaner.i + " apps, ", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (InterruptedException e) {

                        }
                    }
                };
                thread.start();
            }
        });
    }
}

这是UI的XML文件代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/cleanButton"
        android:layout_width="172dp"
        android:layout_height="166dp"
        android:background="@drawable/cleanbutton"
        android:fontFamily="sans-serif"
        android:text="Clean"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="312dp"
        android:layout_height="382dp"
        android:indeterminate="false"
        android:max="0"
        android:progress="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/progressUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="cursive"
        android:text="TextView"
        android:textColor="#0DB344"
        android:textSize="18sp"
        android:textStyle="normal|bold"
        android:typeface="sans"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />

</androidx.constraintlayout.widget.ConstraintLayout>
英文:

I keep getting this error whenever I run the application, which crashes every time I interact with it. The problem first occurred after I moved the original TextView around and altered the color and Font Style. I'm not sure why it's happening now, but it didn't happen before. I'm not exactly sure why this error is occurring either. Please help.

Below is the error message:

E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException
    java.lang.NoSuchMethodException: android.graphics.FontFamily.&lt;init&gt; []

This is the MainActivity class:

package com.example.exploreroptimizer;

import android.app.ActivityManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final Cleaner cleaner = new Cleaner();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button cleanButton = findViewById(R.id.cleanButton);
        final ProgressBar cleanBar = findViewById(R.id.progressBar);
        final TextView progressUpdate = findViewById(R.id.progressUpdate);
        System.out.println(&quot;success&quot;);

        cleanButton.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            final ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
                            ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                            activityManager.getMemoryInfo(mi);
                            final double a = mi.availMem / 0x100000L;

                            cleanBar.setProgress(20);
                            progressUpdate.setText(&quot;Cleaning Ram!&quot;);
                            Thread.sleep(1500);
                            cleanBar.setProgress(40);

                            progressUpdate.setText(&quot;Closing Unused Apps!&quot;);
                            Thread.sleep(1500);
                            cleanBar.setProgress(100);

                            progressUpdate.setText(&quot;Clearing Cache!&quot;);
                            Thread.sleep(1500);
                            Cleaner.deleteCache(getApplicationContext());

                            cleaner.killBackgroundProcesses(getApplicationContext(), getApplicationContext().getPackageManager());
                            cleaner.clearRAM();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {


                                    Toast.makeText(getApplicationContext(), &quot;I have closed &quot; + Cleaner.i + &quot; apps, &quot;, Toast.LENGTH_SHORT).show();


                                }
                            });
                        } catch (InterruptedException e) {

                        }

                    }

                };
                thread.start();

            }
        });
    }


}

And this is the xml file code for the UI:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/cleanButton&quot;

        android:layout_width=&quot;172dp&quot;
        android:layout_height=&quot;166dp&quot;
        android:background=&quot;@drawable/cleanbutton&quot;

        android:fontFamily=&quot;sans-serif&quot;
        android:text=&quot;Clean&quot;
        android:textColor=&quot;#FFFFFF&quot;
        android:textSize=&quot;30sp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;ProgressBar
        android:id=&quot;@+id/progressBar&quot;
        style=&quot;?android:attr/progressBarStyle&quot;
        android:layout_width=&quot;312dp&quot;
        android:layout_height=&quot;382dp&quot;
        android:indeterminate=&quot;false&quot;
        android:max=&quot;0&quot;
        android:progress=&quot;0&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/progressUpdate&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:fontFamily=&quot;cursive&quot;
        android:text=&quot;TextView&quot;
        android:textColor=&quot;#0DB344&quot;
        android:textSize=&quot;18sp&quot;
        android:textStyle=&quot;normal|bold&quot;
        android:typeface=&quot;sans&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/progressBar&quot; /&gt;


&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;



huangapple
  • 本文由 发表于 2020年7月27日 09:33:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/63107534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定