我刚学习android,在书上看到一个示例,就准备运行,可是不知道怎么回事就是运行不了,这个示例是高焕堂老师36计里的COR,可是我不知道怎么运行不了,请帮忙!
谢谢!
由于不知道怎么上传代码,只好把代码贴在下面了。
代码如下:
AndroidManifest<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cor.demo.proj"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ac01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name="Activity_1"></activity>
<activity android:name="Activity_2"></activity>
<activity android:name="Activity_3"></activity>
</application>
</manifest> ac01.javapackage cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class ac01 extends Activity {
public static final int VIEW_ID = Menu.FIRST;
public static final int EDIT_ID = Menu.FIRST + 1;
public static final int PICK_ID = Menu.FIRST + 2;
public static final int EXIT_ID = Menu.FIRST + 3;
private Factory fac;
private SuperTask head;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
fac = new Factory(this); head = fac.get_task_list();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, VIEW_ID, 0, "View");
menu.add(0, EDIT_ID, 1, "Edit");
menu.add(0, PICK_ID, 2, "Pick");
menu.add(0, EXIT_ID, 3, "Exit");
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean ret;
switch (item.getItemId()) {
case VIEW_ID: ret = head.pass("VIEW"); if(!ret) setTitle("false");
break;
case EDIT_ID: ret = head.pass("EDIT"); if(!ret) setTitle("false");
break;
case PICK_ID: ret = head.pass("PICK"); if(!ret) setTitle("false");
break;
case EXIT_ID: finish(); break;
}
return super.onOptionsItemSelected(item);
}
}SuperTask.java
package cor.demo.proj;
import android.app.Activity;
public abstract class SuperTask {
protected SuperTask successor;
abstract boolean pass(String job);
SuperTask(){ successor = null; }
public void add(SuperTask nx){ successor = nx; }
}Factory.java
package cor.demo.proj;
import android.content.Context;
public class Factory {
private SuperTask head;
Factory(Context ctx) {
Task3 t3 = new Task3(ctx); head = t3;
Task2 t2 = new Task2(ctx); t3.add(t2);
Task1 t1 = new Task1(ctx); t2.add(t1);
}
SuperTask get_task_list(){ return head; }
}Task1.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task1 extends SuperTask {
private Context ctx;
Task1(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "VIEW"){ this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
// Intent intent = new Intent(Intent.VIEW_ACTION, null);
Intent intent = new Intent(ctx, Activity_1.class); ctx.startActivity(intent);
}}Task2.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task2 extends SuperTask {
private Context ctx;
Task2(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "EDIT") { this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
//Intent intent = new Intent(Intent.EDIT_ACTION, null);
Intent intent = new Intent(ctx, Activity_2.class); ctx.startActivity(intent);
}}Task3.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task3 extends SuperTask {
private Context ctx;
Task3(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "PICK") { this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
// Intent intent = new Intent(Intent.PICK_ACTION, null);
Intent intent = new Intent(ctx, Activity_3.class);
ctx.startActivity(intent);
}}Activity_1.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_1 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_jude);
btn.setText("Exit fromaction.VIEW");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}
Activity_2.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_2 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_blue);
btn.setText("Exit fromaction.EDIT");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}Activity_3.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_3 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_sky);
btn.setText("Exit fromaction.PICK");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}
谢谢!
由于不知道怎么上传代码,只好把代码贴在下面了。
代码如下:
AndroidManifest<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cor.demo.proj"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ac01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name="Activity_1"></activity>
<activity android:name="Activity_2"></activity>
<activity android:name="Activity_3"></activity>
</application>
</manifest> ac01.javapackage cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class ac01 extends Activity {
public static final int VIEW_ID = Menu.FIRST;
public static final int EDIT_ID = Menu.FIRST + 1;
public static final int PICK_ID = Menu.FIRST + 2;
public static final int EXIT_ID = Menu.FIRST + 3;
private Factory fac;
private SuperTask head;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
fac = new Factory(this); head = fac.get_task_list();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, VIEW_ID, 0, "View");
menu.add(0, EDIT_ID, 1, "Edit");
menu.add(0, PICK_ID, 2, "Pick");
menu.add(0, EXIT_ID, 3, "Exit");
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean ret;
switch (item.getItemId()) {
case VIEW_ID: ret = head.pass("VIEW"); if(!ret) setTitle("false");
break;
case EDIT_ID: ret = head.pass("EDIT"); if(!ret) setTitle("false");
break;
case PICK_ID: ret = head.pass("PICK"); if(!ret) setTitle("false");
break;
case EXIT_ID: finish(); break;
}
return super.onOptionsItemSelected(item);
}
}SuperTask.java
package cor.demo.proj;
import android.app.Activity;
public abstract class SuperTask {
protected SuperTask successor;
abstract boolean pass(String job);
SuperTask(){ successor = null; }
public void add(SuperTask nx){ successor = nx; }
}Factory.java
package cor.demo.proj;
import android.content.Context;
public class Factory {
private SuperTask head;
Factory(Context ctx) {
Task3 t3 = new Task3(ctx); head = t3;
Task2 t2 = new Task2(ctx); t3.add(t2);
Task1 t1 = new Task1(ctx); t2.add(t1);
}
SuperTask get_task_list(){ return head; }
}Task1.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task1 extends SuperTask {
private Context ctx;
Task1(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "VIEW"){ this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
// Intent intent = new Intent(Intent.VIEW_ACTION, null);
Intent intent = new Intent(ctx, Activity_1.class); ctx.startActivity(intent);
}}Task2.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task2 extends SuperTask {
private Context ctx;
Task2(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "EDIT") { this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
//Intent intent = new Intent(Intent.EDIT_ACTION, null);
Intent intent = new Intent(ctx, Activity_2.class); ctx.startActivity(intent);
}}Task3.java
package cor.demo.proj;
import android.content.Context;
import android.content.Intent;
public class Task3 extends SuperTask {
private Context ctx;
Task3(Context tx){ ctx = tx; }
public boolean pass(String job) {
if(job == "PICK") { this.perform(); return true; }
else { if(successor == null) return false;
else return successor.pass(job);
}}
private void perform(){
// Intent intent = new Intent(Intent.PICK_ACTION, null);
Intent intent = new Intent(ctx, Activity_3.class);
ctx.startActivity(intent);
}}Activity_1.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_1 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_jude);
btn.setText("Exit fromaction.VIEW");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}
Activity_2.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_2 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_blue);
btn.setText("Exit fromaction.EDIT");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}Activity_3.java
package cor.demo.proj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_3 extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.act);
Button btn = (Button)findViewById(R.id.btn);
btn.setBackgroundResource(R.drawable.x_sky);
btn.setText("Exit fromaction.PICK");
btn.setOnClickListener(this); }
public void onClick(View arg0) { finish(); }
}
解决方案 »
- android蓝牙通讯问题,调试不报错,直接运行报错???
- 求大神制定学习方向
- 请问在2.3或以下的SDK中如何实现这种“不会被虚键盘移走“的Tab栏呢?
- 关于发送信息加密问题!
- 求助,关于sqlite操作
- [求助]请问如何在一个应用中去使用另一个应用中定义的service?
- 如何使用布局,可以使ListView与兄弟View共同滑动
- android大牛高焕堂最新力作-android架构师之路
- 安卓下,xbox360游戏手柄,怎么让左摇杆不触发方向键消息?
- 求大佬帮忙,我试了好多种办法没搞定
- android应用中动态加载SD 卡的jar文件问题
- 【求助】android--创建helloworld出现的问题
boolean ret;
switch (item.getItemId()) {
case VIEW_ID: ret = head.pass("VIEW"); if(!ret) setTitle("false");
break;
case EDIT_ID: ret = head.pass("EDIT"); if(!ret) setTitle("false");
break;
case PICK_ID: ret = head.pass("PICK"); if(!ret) setTitle("false");
break;
case EXIT_ID: finish(); break;
}
return super.onOptionsItemSelected(item);
}
大概看了一下你的代码 没有发现跳转到其它act的功能啊
在case下面加上相应的跳转语句就可以了
楼主加log debug看看。