嗨,大家好,我有一个简单的问题要解决,不知道为什么我的代码不起作用:当我单击pulsanteinizia崩溃时,我必须使用pulsanteinizia按钮更改活动,但该应用程序
我想从页面1(维护活动)切换到页面2,点击“ pulsanteinizia”按钮
我该如何解决?
第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 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>
最佳答案
检查是否在清单文件中添加了page2
和nationalteams
。如果未添加,请添加并尝试。