0

класс выбивающий приложение:

public class Levels extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.levelsection);

    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Button buttonBack = (Button) findViewById(R.id.buttonBack);
    buttonBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,MainActivity.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });

    TextView textLevelSection = (TextView)findViewById(R.id.textView1);
    textLevelSection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,GameLevels.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });

    TextView textLevelSection2 = (TextView)findViewById(R.id.textView2);
    textLevelSection2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,GameLevels.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });

    TextView textLevelSection3 = (TextView)findViewById(R.id.textView3);
    textLevelSection3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,GameLevels.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });

    TextView textLevelSection4 = (TextView)findViewById(R.id.textView4);
    textLevelSection4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,GameLevels.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });
}

}

главный класс:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonStart = (Button)findViewById(R.id.buttonStart);
    buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent = new Intent(MainActivity.this, Levels.class);
                startActivity(intent);finish();
            }catch (Exception e){

            }
        }
    });

    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yaroslav.firstapp.game">
&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/Theme.Game&quot;&gt;
    &lt;activity
        android:name=&quot;.MainActivity&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.Levels&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.GameLevels&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.Level1&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.Level2&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=&quot;.Level3&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;

    &lt;activity
        android:name=&quot;.Level4&quot;
        android:exported=&quot;true&quot;
        android:screenOrientation=&quot;portrait&quot;&gt;
        &lt;intent-filter&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;


</manifest>

Logcat:

2022-02-02 19:29:58.722 23636-23636/yaroslav.firstapp.game E/AndroidRuntime: FATAL EXCEPTION: main Process: yaroslav.firstapp.game, PID: 23636 java.lang.RuntimeException: Unable to start activity ComponentInfo{yaroslav.firstapp.game/yaroslav.firstapp.game.Levels}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) 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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at yaroslav.firstapp.game.Levels.onCreate(Levels.java:23) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) 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)

строка с ошибкой:

    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Button buttonBack = (Button) findViewById(R.id.buttonBack);
!!!!buttonBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent intent= new Intent(Levels.this,MainActivity.class);
                startActivity(intent);finish();
            }catch (Exception e){
        }
    }
});

  • Пожалуйста, уточните вашу конкретную проблему или приведите более подробную информацию о том, что именно вам нужно. В текущем виде сложно понять, что именно вы спрашиваете. – Дух сообщества Feb 02 '22 at 16:38
  • 1
    В вкладке LogCat будет причина падения – Andrew Feb 02 '22 at 16:40
  • в каком классе? – Yaroslav Nykolyshyn Feb 02 '22 at 16:46
  • это в IDE внизу есть вкладка, не в классе – Andrew Feb 02 '22 at 16:49
  • android:exported="true" и блоки <intent-filter> не нужны активностям, которые вы запускаете сами из своего же приложения – woesss Feb 02 '22 at 16:49
  • activity подчеркивает красным – Yaroslav Nykolyshyn Feb 02 '22 at 16:52
  • Блок - это открывающий тег <intent-filter>, закрывающий </intent-filter> и всё что между ними – woesss Feb 02 '22 at 16:56
  • мне нужно удалить android:exported="true" и в каждой activity ? – Yaroslav Nykolyshyn Feb 02 '22 at 17:00
  • Кроме главной. Если продолжит падать, прочтите это: https://ru.stackoverflow.com/q/797531/11515 и добавьте лог в вопрос – woesss Feb 02 '22 at 17:11
  • я сделал то что посоветовали по ссылке,что не так? – Yaroslav Nykolyshyn Feb 02 '22 at 17:43
  • Судя по логу - в строке Button buttonBack = (Button) findViewById(R.id.buttonBack); вы в итоге записываете null в переменную buttonBack. Скорее всего вы опечатались в ID вашей кнопки. Или в разметке или в коде. – ЮрийСПб Feb 02 '22 at 18:07
  • Спасибо большое! заплутался с id – Yaroslav Nykolyshyn Feb 02 '22 at 18:32

0 Answers0