gpt4 book ai didi

android - 开启第二个 Activity 失败

转载 作者:行者123 更新时间:2023-11-29 15:40:14 24 4
gpt4 key购买 nike

所以我试图通过单击按钮从第一个 Activity 中打开第二个 Activity 。该应用程序已加载,但是当我尝试按下按钮以转到下一个 Activity 时,没有任何反应。这里分别是我的 main.xml、mainactivity.java、androidmanifest.xml、shopactivity.java 和 shop.xml。非常感谢任何帮助。

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/counter"
android:textColor="#000000"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="@string/gems"
android:textColor="#000000"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView1"
android:layout_alignBaseline="@+id/textView1"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView2"
android:layout_alignBaseline="@+id/textView2"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000000"
android:text="@string/button"
android:textColor="#ffffff"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:background="#ffffff"
android:text="@string/button2"
android:textColor="#000000"
android:onClick="onClickShop"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentRight="true"
android:background="#ffffff"
android:text="@string/button3"
android:textColor="#000000"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/button2"
android:layout_toLeftOf="@+id/button3"
android:background="#ffffff"
android:text="@string/button4"
android:textColor="#000000"/>
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

TextView textView1;
TextView textView2;
EditText editText1;
EditText editText2;
Button button1;
Button button2;
Button button3;
Button button4;
int counter = 0;
int gems = 0;

@Override
public void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);

textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);}

@Override
protected void onStop()
{super.onStop();
SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit(); editor.putInt("key", counter); editor.commit();}

@Override
protected void onStart()
{super.onStart(); SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE);
int defaultValue = 0;
counter = prefs.getInt("key", defaultValue);
editText1.setText(Integer.toString(counter));}

@Override
public void onClick(View v)
{if (v == button1)
{counter++; editText1.setText(Integer.toString(counter));}}

public void onClickShop(View v)
{if (v == button2)
{
Intent intent = new Intent(MainActivity.this, ShopActivity.class);
startActivity(intent);
}}}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bugagullgames.clicker" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ShopActivity"
android:label="@string/app_name">
</activity>
</application>

</manifest>

public class ShopActivity extends MainActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
}}

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:text="Welcome to the shop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>

</RelativeLayout>

最佳答案

您正在覆盖 button2 的 onclick 监听器。删除这行代码:

button2 = (Button)findViewById(R.id.button2);

您配置的 xml onclick 属性 android:onClick="onClickShop" 应该可以工作。

关于android - 开启第二个 Activity 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41706934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com