0

我正在阅读 Hello Android 书,我正在 eclipse 中编写书中的代码,但按钮不起作用。这是为什么?

数独/src/org.example.sudoku/Sudoku.java

package org.example.sudoku;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Sudoku extends Activity implements OnClickListener {
private static final String TAG = "Sudoku";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up click listeners for all the buttons
    View continueButton = findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);
    View newButton = findViewById(R.id.new_button);
    newButton.setOnClickListener(this);
    View aboutButton = findViewById(R.id.about_button);
    aboutButton.setOnClickListener(this);
    View exitButton = findViewById(R.id.exit_button);
    exitButton.setOnClickListener(this);
 }



 // ...
 public void onClick(View v) {
     switch (v.getId()) {
     case R.id.about_button:
         Intent i = new Intent(this, About.class);
         startActivity(i);
         break;
     // More buttons go here (if any) ...

     }
  }
}

数独/src/org.example.sudoku/About.java

package org.example.sudoku;

import android.app.Activity;
import android.os.Bundle;

public class About extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}
 }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.sudoku"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

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

                  <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
        </activity>
        <activity android:name=".About"
            android:label="@string/about_label">     
        </activity>            

    </application>

 </manifest>

我单击关于按钮,但不工作且不运行活动。如果您需要其他程序文件,请告诉我。请帮我。谢谢你。

4

4 回答 4

0

试试这个。在您编写 MainActivity 的清单文件中,请将其更改为数独。

对于此错误无法从视图转换为按钮

您需要将视图转换为 Button:

Button aboutButton  = (Button)findViewById(android.R.id.aboutButton );
于 2012-09-01T12:13:46.847 回答
0

您可以尝试使用特定的 Button 类:

Button aboutButton = (Button) findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
于 2012-09-01T12:26:49.030 回答
0

首先将所有视图更改为按钮

Button continueButton = (Button) findViewById(R.id.continue_button);

AND SECOND 将点击事件中的活动上下文作为

public void onClick(View v) {
     switch (v.getId()) {
     case R.id.about_button:
         Intent i = new Intent(Sudoku.this, About.class);
    /////////       change here ^^^^^^^^^^^     
         startActivity(i);
         break;
     // More buttons go here (if any) ...

     }
 }
于 2012-09-01T12:29:44.397 回答
0

在 activity_main.xml 文件中,你应该声明你的按钮,

<Button
    android:id="@+id/about_button"
    android:layout_width="319dp"
    android:layout_height="wrap_content"
    android:text="About" />

然后在你的课上,sudoku.java,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up click listeners for all the buttons

    Button aboutButton = (Button) findViewById(R.id.about_button);
    aboutButton.setOnClickListener(new View.OnClickListener() 
{
            @Override
            public void onClick(View v) 
            {
                            //Code for whatever you want to perform on this button click
                        }
 }
于 2012-09-03T07:52:08.623 回答