我正在尝试使用一个简单的按钮来在屏幕之间导航,我真的不确定我做错了什么,因为我按照指南进行操作!
第一页代码:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button aButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} {
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener( {;new OnClickListener();
}
public void onClick(View v){
Intent i = new Intent(mainactivity.this, buttons2.class); startActivity(i);
} }
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.xx.MainActivity"
android:label="@string/app_name" >
android:label="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Buttons2" android:label="Buttons2"></activity>
</application>
</manifest>
第二页代码:
import android.app.Activity;
import android.os.Bundle;
public class Buttons2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
} }
请帮忙!
谢谢!
答案 0 :(得分:1)
尝试重新格式化代码。有很多方括号。
它应该以这种方式工作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(mainactivity.this, buttons2.class);
startActivity(i);
}
});
}
答案 1 :(得分:0)
您的问题似乎出现在onCreate
方法中。您正在关闭onCreate
和},然后尝试通过findViewById
找到按钮。由于这不包含在方法中,因此从不调用它。尝试这样的事情:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v){
Intent i = new Intent(mainactivity.this, buttons2.class);
startActivity(i);
});
}
答案 2 :(得分:0)
您的MainActivity块遇到严重问题:o
public class MainActivity extends Activity {
Button aButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Buttons2.class);
startActivity(i);
}
});
}
}
答案 3 :(得分:0)
MainActivity的onCreate()是问题所在。它看起来应该是这样的。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener(new OnClickListener();
tb.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Buttons2.class);
startActivity(i);
}
});
还要确保activity_main.xml有一个带
的按钮android:id="@+id/R.id.scales"
答案 4 :(得分:0)
首先,在您的案例中,您不需要在findViewById();
之前写这个。并进行以下更正:
public class MainActivity extends Activity {
Button aButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.scales);
aButton.setOnClickListener( new OnClickListener()
public void onClick(View v){
Intent i = new Intent(mainactivity.this, buttons2.class);
startActivity(i);
});
}
}
其余的事情是正确的。