Android startActivity方法没有';t在real device中启动新活动

Android startActivity方法没有';t在real device中启动新活动,android,android-intent,android-activity,start-activity,Android,Android Intent,Android Activity,Start Activity,我是Android的初学者。我试图通过onclick事件中的意图开始一项新活动。这在emulator中运行良好。但是当我在真实的设备上尝试它时,它不起作用,应用程序再次进入主屏幕。logcat中未显示任何错误 这就是我调用startActivity方法的地方 relativeAbout.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TOD

我是Android的初学者。我试图通过onclick事件中的意图开始一项新活动。这在emulator中运行良好。但是当我在真实的设备上尝试它时,它不起作用,应用程序再次进入主屏幕。logcat中未显示任何错误

这就是我调用
startActivity
方法的地方

relativeAbout.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                Class OurClass = Class
                        .forName("com.wad.tourismguide.AboutCity");
                Intent newIntent = new Intent(getApplicationContext(),
                        OurClass);


                newIntent.putExtra("name", name);
                newIntent.putExtra("detail", detail);
                newIntent.putExtra("image", main);
                startActivity(newIntent);



                System.out.println("intent starting");
            } catch (ClassNotFoundException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });
新活动如下所示

    public class AboutCity extends Activity {
    TextView cityName;
    ImageView image;
    TextView detailText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.aboutcity);

        cityName = (TextView)findViewById(R.id.tvDetailCityName);
        image = (ImageView)findViewById(R.id.ivDetailImage);
        detailText = (TextView)findViewById(R.id.tvDetailText);

        String name = getIntent().getStringExtra("name");
        System.out.println("city name"+ name);
        String detail = getIntent().getStringExtra("detail");
        System.out.println("city detail"+ detail);
        Bitmap b= (Bitmap)getIntent().getParcelableExtra("image");
        System.out.println(detail);
        cityName.setText(name);
        detailText.setText(detail);
        image.setImageBitmap(b);
    }


}

正如我前面解释的,这在模拟器中工作得很好。但它在真正的设备中不起作用。我找不到哪里出了问题。有人能帮我一下吗?

除非有特殊原因使用
类OurClass=Class.forName(“com.wad.tourismguide.AboutCity”)
要设置目标
,通过Intent调用另一个活动的传统方式(使用
附加
)将按照广告的方式工作

Intent newIntent = new Intent(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);
您还可以尝试另一个与上述代码基本相同的变体:

Intent newIntent = new Intent();
newIntent.setClass(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

编辑:读完这篇:,我倾向于认为
Class-OurClass=Class.forName(“com.wad.tourismguide.AboutCity”)根本不需要。如果我在这一点上错了,有人可以纠正我。

除非有特定的理由使用
类OurClass=Class.forName(“com.wad.tourismguide.AboutCity”)
要设置目标
,通过Intent调用另一个活动的传统方式(使用
附加
)将按照广告的方式工作

Intent newIntent = new Intent(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);
您还可以尝试另一个与上述代码基本相同的变体:

Intent newIntent = new Intent();
newIntent.setClass(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

编辑:读完这篇:,我倾向于认为
Class-OurClass=Class.forName(“com.wad.tourismguide.AboutCity”)根本不需要。如果我在这一点上错了,有人可以纠正我。

只需将您当前的活动类和要启动的活动类传递给构造函数:

Intent intent = new Intent(CurrentActivity.this, AboutCity.class);
// put extra
startActivity(intent);

只需将当前活动类和要启动的活动类传递给构造函数:

Intent intent = new Intent(CurrentActivity.this, AboutCity.class);
// put extra
startActivity(intent);

我不认为您的代码是错误的,但您可以在调用新的意图时对其进行简化:

Intent newIntent = new Intent(CurrentActivity.this, AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

毕竟,您的代码将更加清晰,并且您不需要看到异常(错误机会更少)。

我不认为您的代码是错误的,但您可以在调用新的意图时简化代码:

Intent newIntent = new Intent(CurrentActivity.this, AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

毕竟,您的代码将更加清晰,并且您不需要看到异常(错误机会更少)。

也发布错误日志。还有,使用这个来声明类是否有原因:
classourclass
?您是否收到ClassNotFoundException?。。。如果您的com.wad.tourismguide.AboutCity包和类名都很完美,那么它就无法正常工作。。。在这里,您正在捕获ClassNotFoundException,因此如果类路径错误,它将抛出异常并简单地停留在MainActivity中…也发布错误日志。还有,使用这个来声明类是否有原因:
classourclass
?您是否收到ClassNotFoundException?。。。如果您的com.wad.tourismguide.AboutCity包和类名都很完美,那么它就无法正常工作。。。在这里,您正在捕获ClassNotFoundException,因此如果类路径错误,它将抛出异常并停留在MainActivity中…为您的答案提供一些解释。为您的答案提供一些解释。