1

我对android编程完全陌生,我正在读一本名为“Hello Android”的书

基本上,这本书通过使用数独游戏示例来教我们 Android。

这是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:padding="30dip"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center" >
<TextView
android:text="@string/main_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp" />
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label" />
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:id="@+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:id="@+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>

这是string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
<color name="background">#3500ffff</color>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>

这是旧版本的附加清单:

<activity android:name=".About"
android:label="@string/about_title" >
        </activity>

这是 about.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>

最后是 Sudoku.java

package org.example.sudoku;

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

public class Sudoku extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener((OnClickListener) continueButton);

        View newButton = findViewById(R.id.new_button);
        newButton.setOnClickListener((OnClickListener) this);

        View aboutButton = findViewById(R.id.about_button);
        aboutButton.setOnClickListener((OnClickListener) this);

        View exitButton = findViewById(R.id.exit_button);
        exitButton.setOnClickListener((OnClickListener) this);


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

}

让我稍微解释一下。数独主页面由 4 个按钮组成,这本书正在教我们实现按钮,所以当用户按下它时,程序会引导用户到 about 页面(这只是一个文本页面)。错误发生在About.class上,Eclipse说没有这个类叫About。而且我不太明白为什么在意图参数中还有一个 About.class ......

和想法?

4

3 回答 3

1

根据您的评论........您must have the file About.java in you project还可以在清单中添加它的条目...

<activity android:name=".About"
        android:label="@string/app_name">

    </activity>

所以在你的项目中将 About.java 与 Sudoku.java 并行添加,它还需要扩展 Activity ......

于 2012-06-06T13:54:29.850 回答
1

您是否在清单中写下您的活动,使用与其他活动正确或相同的包?

http://developer.android.com/guide/topics/manifest/manifest-intro.html

您能否也请张贴您的日志猫?

于 2012-06-06T13:47:23.753 回答
1

About.javaorg.example.sudoku包里上课吗?

public class About extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
 }
}
于 2012-06-06T13:48:50.263 回答