[英]Android Studio Other Activities can't be opened
Hi guys I've a simple problem to resolve, don't know why my code doesn't works: I have to change activity with the button pulsanteinizia but the app, when I click on pulsanteinizia crash I want to switch from to page1 (mainactivity) to page 2 clicking the button "pulsanteinizia"大家好,我有一个简单的问题要解决,不知道为什么我的代码不起作用:我必须使用按钮 pulsanteinizia 但应用程序更改活动,当我单击 pulsanteinizia 崩溃时,我想从第 1 页切换到第 1 页( mainactivity) 到第 2 页,单击“pulsanteinizia”按钮
How can I resolve that?我该如何解决?
PAGE 1 (MAINACTIVITY)第 1 页(主要活动)
package com.example.footballplayersquiz2020;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class page1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pulsanteinizia=(Button) findViewById(R.id.pulsanteinizia);
Button pulsantenazionali=(Button) findViewById(R.id.pulsantenazionali);
TextView record =(TextView) findViewById(R.id.record);
TextView recordn =(TextView) findViewById(R.id.recordn);
ImageView descrizione=(ImageView)findViewById(R.id.descrizione);
Drawable welcome1 =getResources().getDrawable(R.drawable.welcome1);
Drawable welcome2 =getResources().getDrawable(R.drawable.welcome2);
Drawable welcome3 =getResources().getDrawable(R.drawable.welcome3);
Drawable welcome4 =getResources().getDrawable(R.drawable.welcome4);
SharedPreferences settings = getSharedPreferences("GAME_DATA", Context.MODE_PRIVATE);
SharedPreferences settingsn = getSharedPreferences("GAME_DATAN", Context.MODE_PRIVATE);
int highscore = settings.getInt("HIGH_SCORE", 0);
int highscoren = settingsn.getInt("HIGH_SCOREN", 0);
record.setText("Record: " + highscore + " punti");
recordn.setText("Record Nazionali: " + highscoren + " punti");
if (highscore > 0 && highscore < 30) descrizione.setImageDrawable(welcome2);
else if (highscore >= 30 && highscore < 100) descrizione.setImageDrawable(welcome3);
else if (highscore >= 100) descrizione.setImageDrawable(welcome4);
pulsanteinizia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent p2 = new Intent(getApplicationContext(),page2.class);
startActivity(p2);
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_out_left);
finish();
}
});
pulsantenazionali.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String ITEM_SKU="com.nationalteams.active";
final Intent pn = new Intent(getApplicationContext(),nationalteams.class);
startActivity(pn);
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_out_left);
finish();
}
});
}
}
XML: 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=".page1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sfondo1"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/logo2019.png"
android:src="@drawable/logo2019"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/descrizione"
android:src="@drawable/welcome1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/schermo1buttons"
android:id="@+id/pulsanteinizia"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/schermo1buttons2"
android:id="@+id/pulsantenazionali"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:textColor="#FFFF00"
android:textStyle="bold"
android:layout_marginRight="145dp"
android:layout_height="wrap_content"
android:text="Record: 0 punti"
android:id="@+id/record"/>
<TextView
android:textColor="#FFFF00"
android:id="@+id/recordn"
android:textStyle="bold"
android:layout_marginRight="113dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record Nazionali: 0 punti"/>
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
check if you have added page2
and nationalteams
in your manifest file.检查您是否在清单文件中添加了
page2
和nationalteams
。 If not added, add it and try.如果没有添加,添加它并尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.