1
I made an app in Android Studio, according to codes below, however, when emulating inside an emulator of Android Studio itself, does not run. There seems to be some problem in the Activity instance, but the manifest seems to be right, just like onCreate in the java class. Then I don’t understand what happens! Error with some package? Some poorly instantiated code? I don’t know!
Error on Run screen:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.dimtecarte.www.jumper, PID: 5836
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.dimtecarte.www.jumper/br.com.dimtecarte.www.jumper.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:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:87)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:577)
at android.view.View.<init>(View.java:3778)
at android.view.SurfaceView.<init>(SurfaceView.java:184)
at br.com.dimtecarte.www.jumper.Game.<init>(Game.java:15)
at br.com.dimtecarte.www.jumper.MainActivity.<init>(MainActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
java game.:
package br.com.dimtecarte.www.jumper;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;//Esse SurfaceView é para criar uma tela onde calcularemos a posição de cada objeto.
public class Game extends SurfaceView implements Runnable{
private final SurfaceHolder holder = getHolder(); //Nos dá acesso ao canvas, permitindo a edição de cada pixel nos cenários.
private Passaro passaro;
public Game(Context context){
super(context);
inicializaElementos();
}
public boolean isRunning = true; //Dirá se o app está rodando.
@Override
public void run() {
while (isRunning){ //Se verdadeiro, executará o bloco abaixo repetidadamente.
if(!holder.getSurface().isValid()) continue; //Se o SurfaceHolder estiver pronto para desenhar, pode continuar.
Canvas canvas = holder.lockCanvas();
passaro.desenhaNo(canvas);
holder.unlockCanvasAndPost(canvas); //Depois que terminar de desenhar a tela, ela substituirá a tela antiga.
}
}
public void cancela(){ //Vai cancelar a execução do app para para o while acima. Isto é importante para não sobrecarregar o uso do aparelho.
this.isRunning=false;
}
public void inicia(){ //Vai retomar a execução do app. O while voltará a ser executado.
this.isRunning=true;
}
public void inicializaElementos(){
this.passaro = new Passaro();
}
public class Passaro{
private final Paint vermelho = Cores.getCorDoPassaro(); //Obtendo a cor.
private static final int X = 100;
private static final int RAIO = 50;
private final int altura;
public Passaro(){
this.altura=100;
}
public void desenhaNo(Canvas canvas){
canvas.drawCircle(X, altura, RAIO,vermelho); //Vai desenhar um círculo vermelho.
}
}
public static class Cores{
public static Paint getCorDoPassaro() {
Paint vermelho = new Paint();
vermelho.setColor(0xFFFF0000);
return vermelho;
}
}
}
activity_main.xml:
<FrameLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Mainactivity.java:
package br.com.dimtecarte.www.jumper;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
Game game = new Game(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
FrameLayout container = findViewById(R.id.container);
container.addView(game);
}
//-------------------------------MÉTODOS DO CICLO DE VIDA DA ACTIVITY--------------------------------------------------------------------
@Override
protected void onPause(){ //Quando o app for pausado, vai determinar o que será feito.
super.onPause();
game.cancela();
}
@Override
protected void onResume(){ //Quando o app entrar em primeiro plano novamente, vai determinar o que acontecerá.
super.onResume();
game.inicia();
new Thread(game).start();
}}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.dimtecarte.www.jumper">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Design.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
12-30 11:18:22.961 1693-1693/system_process E/Locksettingsstorage: Cannot read file java.io.Filenotfoundexception: /data/system/gatekeeper.password.key: open failed: ENOENT (No such file or directory)
12-30 11:18:22.961 1693-1693/system_process E/Locksettingsstorage: Cannot read file java.io.Filenotfoundexception: /data/system/password.key: open failed: ENOENT (No such file or directory)
12-30 11:18:22.961 1693-1693/system_process E/Locksettingsstorage: Cannot read file java.io.Filenotfoundexception: /data/system/gatekeeper.pattern.key: open failed: ENOENT (No such file or directory)
12-30 11:18:22.962 1693-1693/system_process E/Locksettingsstorage: Cannot read file java.io.Filenotfoundexception: /data/system/gatekeeper.gesture.key: open failed: ENOENT (No such file or directory)
12-30 11:18:22.963 1693-1693/system_process E/Locksettingsstorage: Cannot read file java.io.Filenotfoundexception: /data/system/Gesture.key: open failed: ENOENT (No such file or directory)