0

我正在尝试获得一个简单的按钮来在屏幕之间导航,我真的不确定我做错了什么,因为我遵循了指南!

第一页代码:

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);
} }

请帮忙!

谢谢!

4

5 回答 5

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);
       } 
    });
}
于 2013-04-30T12:07:01.123 回答
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);
            }
        });
    } 
}
于 2013-04-30T12:08:24.410 回答
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 有一个 Button

android:id="@+id/R.id.scales"
于 2013-04-30T12:08:53.650 回答
0

你的问题似乎出在你的onCreate方法上。您正在关闭onCreatewith },然后尝试通过findViewById. 由于 this 不包含在方法中,因此它永远不会被调用。尝试这样的事情:

@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);
     }); 
 }
于 2013-04-30T12:06:33.973 回答
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);
        });
    }
}

其余的事情是正确的。

于 2013-04-30T12:19:11.350 回答