我对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) ...
}
}
}
让我解释一下。 Sudoku主页由4个按钮组成,本书教我们实现按钮,所以当用户按下它时,程序会将用户引导到about页面(这只是一页文本)。 错误发生在About.class上,Eclipse说没有这样的类叫做About。而且我不太明白为什么在intent参数中也有一个About.class ....
并且想法??
答案 0 :(得分:1)
您是否使用与其他活动相同或相同的套餐在清单中记下了您的活动?
http://developer.android.com/guide/topics/manifest/manifest-intro.html
你还能发贴你的日志猫吗?
答案 1 :(得分:1)
您在About.java
包中有org.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);
}
}
答案 2 :(得分:1)
根据你的评论........你must have the file About.java in you project
并在清单中添加它的条目......
<activity android:name=".About"
android:label="@string/app_name">
</activity>
所以在你的项目中将Sud.java与Sudoku.java并行添加,它还需要扩展Activity .........