2012-09-01 26 views
0

我正在閱讀你好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> 

我點擊關於按鈕,但不工作,不運行活動。如果你需要另一個程序文件,告訴我。 請幫幫我。 謝謝。

+0

你有'MainActivity'類嗎?您的清單聲明最初啓動「MainActivity」。也許你打算把你的數獨活動放在清單中? – brianestey

回答

0

試試這個。 在您編寫MainActivity的清單文件中,請將其更改爲Sudoku。

對於這個錯誤不能查看轉換爲按鈕

你需要轉換的視圖按鈕:

Button aboutButton = (Button)findViewById(android.R.id.aboutButton); 
+0

我可否知道投票表決的原因? –

+0

按鈕本身就是一個View,所以如果OP爲View設置click事件,會出現什麼問題? –

+0

謝謝。我添加它並導入android.widget.Button; Sudoku.java和程序有錯誤:類型不匹配:無法從視圖轉換爲按鈕。 –

0

您可以嘗試使用特定的Button類:

Button aboutButton = (Button) findViewById(R.id.about_button); 
aboutButton.setOnClickListener(this); 
0

首先將所有查看按鈕更改爲按鈕

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

然後點擊事件活動作爲

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) ... 

    } 
} 
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 
         } 
} 
相關問題