如果处理程序调用其中一个活动,如何在 Android 中的活动之间切换

发布于 2024-12-07 19:23:33 字数 2075 浏览 0 评论 0原文

当我调用它时,我的问题就出现了,

 Intent i = new Intent(c.getApplicationContext(),ActivityMulti.class); 
 c.startActivity(i);                     

它会生成下面提到的异常:

10-02 17:54:26.037: ERROR/AndroidRuntime(905): java.lang.RuntimeException:  Unable to instantiate activity ComponentInfo{package.com/package.ActivityMulti}:  
java.lang.InstantiationException: package.ActivityMulti

这在处理程序中被调用,生成如下:

Message msg = handler.obtainMessage();
Bundle b = new Bundle();    
msg.setData(b);
handler.sendMessage(msg);   

第二个活动是一个简单的活动:

public class ActivityMulti extends Activity{

    public ActivityMulti(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        init();
    }
    private void init(){
         TextView tv = new TextView(this);
         tv.setText("This is activity multi");
         setContentView(tv);
        //ImageButton info = (ImageButton)findViewById(R.id.info);
        //info.setImageResource(R.drawable.info2);
        //this.setContentView(R.layout.view_multi);
    }

    }

为什么会生成该异常?你能帮助我吗?

编辑: 这是我的清单:

.
.
.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMulti"></activity>
</application>
</manifest>                                                    

编辑 2:

我解决了!问题出在第二个 Activity 的构造函数中。没有必要!

public class ActivitySingle extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
}
private void init(){
    TextView tv = new TextView(this);
    tv.setText("This is activity single");
    setContentView(tv);
    //ImageButton info = (ImageButton)findViewById(R.id.info);
    //info.setImageResource(R.drawable.info2);
    //this.setContentView(R.layout.view_multi);
}
}

My problem borns when i call

 Intent i = new Intent(c.getApplicationContext(),ActivityMulti.class); 
 c.startActivity(i);                     

It generates the exception mentioned below:

10-02 17:54:26.037: ERROR/AndroidRuntime(905): java.lang.RuntimeException:  Unable to instantiate activity ComponentInfo{package.com/package.ActivityMulti}:  
java.lang.InstantiationException: package.ActivityMulti

This is called in an handler generate like this:

Message msg = handler.obtainMessage();
Bundle b = new Bundle();    
msg.setData(b);
handler.sendMessage(msg);   

The second Activity is a simple one:

public class ActivityMulti extends Activity{

    public ActivityMulti(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        init();
    }
    private void init(){
         TextView tv = new TextView(this);
         tv.setText("This is activity multi");
         setContentView(tv);
        //ImageButton info = (ImageButton)findViewById(R.id.info);
        //info.setImageResource(R.drawable.info2);
        //this.setContentView(R.layout.view_multi);
    }

    }

Why that exception is generated? Can you help me?

EDIT:
This is my manifest:

.
.
.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMulti"></activity>
</application>
</manifest>                                                    

EDIT 2:

I Solved! the problem was in constructor of the second Activity.. There is no need of it!

public class ActivitySingle extends Activity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
}
private void init(){
    TextView tv = new TextView(this);
    tv.setText("This is activity single");
    setContentView(tv);
    //ImageButton info = (ImageButton)findViewById(R.id.info);
    //info.setImageResource(R.drawable.info2);
    //this.setContentView(R.layout.view_multi);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-12-14 19:23:33

您是否已在清单文件中添加了要启动的活动?

如果您从 Handler 启动它并不重要,因为您已经为使用 .startActivity() 提供了正确的上下文。我认为您错过了在清单中添加活动。

Have you added the activity you are starting in the Manifest file?

It's not important if you are starting it from Handler, since you have provided the right context for using .startActivity(). I think you have missed to add the activity in the manifest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文