所以我试图通过点击按钮从第一个活动中打开第二个活动。该应用程序加载,但当我尝试按下按钮进入下一个活动时没有任何反应。这是我的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>
答案 0 :(得分:4)
您正在覆盖button2 onclick侦听器。删除这行代码:
button2 = (Button)findViewById(R.id.button2);
您配置onclick
的xml android:onClick="onClickShop"
属性应该有效。
答案 1 :(得分:0)
如果你想使用xml方法。
你的声明方法必须像它一样。 public void methodname(查看v)
这个论点很重要。
答案 2 :(得分:0)
覆盖button2 onclick侦听器。本节目的程序化;
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickShop(v);
}
});
答案 3 :(得分:0)
替换{if (v == button2)
{if (v == button2.getView())
或尝试按ID查找。
我建议使用:
button2.setOnclickListener(new OnClickListener {
// TO DO
})
答案 4 :(得分:0)
你好@Bugagull在你的清单中定义你的“ShopActivity.class”。
<activity
android:name=".ShopActivity"
android:label="@string/app_name">
</activity>
并替换它。
public class ShopActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
}
}