@klen007

Здравствуйте, я программирую в андроид студио, внезапно стали появляться ошибки при запуске в чём дело, как пофиксить?

Ошибка Render problem в XML
И следующие ошибки при запуске:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.stortor.tolyapp, PID: 7966
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.stortor.tolyapp/com.stortor.tolyapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:97)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:607)
at android.media.SoundPool.load(SoundPool.java:216)
at com.stortor.tolyapp.MainActivity.(MainActivity.java:86)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
  • Вопрос задан
  • 156 просмотров
Пригласить эксперта
Ответы на вопрос 1
@klen007 Автор вопроса
Код 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:id="@+id/bottomRaz"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/i"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/bottomQuest"
        style="@style/Widget.MaterialComponents.TimePicker.Button"
        android:layout_width="0dp"
        android:layout_height="74dp"
        android:layout_marginTop="156dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/i"
        android:text="Квесты"
        android:textAllCaps="false"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/black"
        android:textSize="50dp"
        android:textStyle="normal"
        app:layout_constraintBottom_toTopOf="@+id/bottomSettings"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/bottomSettings"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="0dp"
        android:layout_height="74dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="25dp"
        android:background="@drawable/i"
        android:text="Настройки"
        android:textColor="@color/black"
        android:textSize="50dp"
        app:iconTint="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/buttonCreators"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bottomQuest"
        app:layout_constraintVertical_bias="0.0"
        app:rippleColor="@color/black"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/buttonCreators"
        style="@style/Widget.MaterialComponents.TimePicker.Button"
        android:layout_width="428dp"
        android:layout_height="79dp"
        android:layout_marginBottom="200dp"
        android:background="@drawable/i"
        android:text="Разработчики"
        android:textColor="@color/black"
        android:textSize="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.529"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bottomSettings"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/PlayButton"
        style="@android:style/Widget.DeviceDefault.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="207dp"
        android:onClick="play"
        android:text="Play"
        app:layout_constraintBottom_toBottomOf="@id/bottomQuest"
        app:layout_constraintEnd_toStartOf="@+id/pauseButton"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/pauseButton"
        style="@android:style/Widget.Material.Button.Borderless"
        android:layout_width="105dp"
        android:layout_height="48dp"
        android:layout_marginStart="90dp"
        android:layout_marginEnd="206dp"
        android:layout_marginBottom="133dp"
        android:onClick="pause"
        android:text="Pause"
        app:layout_constraintBottom_toTopOf="@id/bottomQuest"
        app:layout_constraintEnd_toEndOf="@id/stopButton"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@id/PlayButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/stopButton"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="113dp"
        android:layout_marginBottom="131dp"
        android:onClick="stop"
        android:text="Stop"
        app:layout_constraintBottom_toTopOf="@+id/bottomQuest"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/pauseButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java код:
package com.stortor.tolyapp;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    public Button bottomQuest;
    public Button bottomSettings;
    public Button buttonCreators;
    public Button playButton;
    public Button pauseButton;
    public Button stopButton;


    final int MAX_STREAMS = 100;
    MediaPlayer mediaPlayer1;
    int list;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer1 = MediaPlayer.create(this, R.raw.dragon);
        mediaPlayer1.setOnCompletionListener(mp -> stopPlay());
        playButton = findViewById(R.id.PlayButton);
        pauseButton = findViewById(R.id.pauseButton);
        stopButton = findViewById(R.id.stopButton);

        pauseButton.setEnabled(false);
        stopButton.setEnabled(false);
    }

    public void stopPlay() {
        mediaPlayer1.stop();
        pauseButton.setEnabled(false);
        stopButton.setEnabled(false);
        try {
            mediaPlayer1.prepare();
            mediaPlayer1.seekTo(0);
            playButton.setEnabled(true);
        } catch (Throwable t) {
            Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    public void play(View
                             view) {

        mediaPlayer1.start();
        playButton.setEnabled(false);
        pauseButton.setEnabled(true);
        stopButton.setEnabled(true);
    }

    public void pause(View view) {

        mediaPlayer1.pause();
        playButton.setEnabled(true);
        pauseButton.setEnabled(false);
        stopButton.setEnabled(true);
    }

    public void stop(View view) {
        stopPlay();
    }

    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer1.isPlaying()) {
            stopPlay();
        }
    }

    {

        SoundPool sp2 = new SoundPool(MAX_STREAMS, AudioManager.STREAM_ALARM, 1);
        list = sp2.load(this, R.raw.listanie, 2);
        bottomQuest = (Button) findViewById(R.id.bottomQuest);
        bottomQuest.setOnClickListener(view -> {
            sp2.play(1, 0, 1, 100, 0, 2);
            Intent intent = new Intent(MainActivity.this, QuestActivity.class);
            startActivity(intent);
        });
        bottomSettings = (Button) findViewById(R.id.bottomSettings);
        bottomSettings.setOnClickListener(view -> {
            sp2.play(1, 0, 1, 100, 0, 2);
            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(intent);
        });

        buttonCreators = (Button) findViewById(R.id.buttonCreators);
        buttonCreators.setOnClickListener(view -> {
            sp2.play(1, 0, 1, 100, 0, 2);
            Intent intent = new Intent(MainActivity.this, CreatorsActivity.class);
            startActivity(intent);
        });


    }
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы