我刚学习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(); }
}

解决方案 »

  1.   

    出现的问题是:在eclipse上运行后,在模拟器上,点击MENU后,点击VIEW,EDIT等,程序就被迫关闭了,没有迁移到相应的画面。
      

  2.   

    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);
            }
    大概看了一下你的代码 没有发现跳转到其它act的功能啊
      

  3.   

    单击菜单时要处理相应的跳转事件
    在case下面加上相应的跳转语句就可以了
      

  4.   

    看代码没问题,菜单的点击处理也做了activity的跳转,AndroidManifest.xml中也加入了所有activity。
    楼主加log debug看看。
      

  5.   

    你的把你的错误log抓出来看下就一目了然了
      

  6.   

    把log发出来看看吧。看了下代码没发现啥错误。
      

  7.   

    是啊,把你logcat里面的log贴出来看看啊。