切换到新的活动/应用程序页面时应用程序崩溃
当我使用菜单按钮切换到新页面并调用新活动时,我的应用程序崩溃了。我在几个应用程序中编写了相同的菜单按钮/活动,但从未遇到过问题。
我的应用程序中有以下类:
- SplashActivity.java
- MainActivity.java
- AboutUs.Java
我已经通过重新打开 SplashActivity 测试了菜单按钮并且它有效,因此错误不在于菜单按钮和调用该函数。我还更改了 AboutUs.java 的内容,只包含导入和 R.id.about_us xml,但它仍然不起作用,这让我认为页面内没有错误。
我的 LogCat 错误:
ERROR/AndroidRuntime(667):
ERROR: thread attach failed
08-04 12:07:13.039:
ERROR/AndroidRuntime(675):
ERROR: thread attach failed
08-04 12:07:19.119:
ERROR/gralloc(68): [unregister] handle 0x3ea8d8 still locked (state=40000001)
08-04 12:07:23.489:
ERROR/global(685): Deprecated Thread methods are not supported.
08-04 12:07:23.489:
ERROR/global(685): java.lang.UnsupportedOperationException
08-04 12:07:23.489:
ERROR/global(685): at java.lang.VMThread.stop(VMThread.java:85)
08-04 12:07:23.489:
ERROR/global(685): at java.lang.Thread.stop(Thread.java:1379)
08-04 12:07:23.489:
ERROR/global(685): at java.lang.Thread.stop(Thread.java:1344)
08-04 12:07:23.489:
ERROR/global(685): at com.peakmobiledesigns.kitchenunitconverter.SplashActivity$1.run(SplashActivity.java:28)
MainActivity 中调用 AboutUs.java 的菜单 java:
//menu starts here
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case R.id.feedback:
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]"[email protected]"});
startActivity(intent);
return true;
case R.id.about_us:
startActivity(new Intent("com.peakmobiledesigns.kitchenunitconverter.ABOUTUS"));
return true;
}
return false;
}
//menu ends here
这是我在 Android 清单中为该活动列出的内容:
<activity android:name=".AboutUs" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.peakmobiledesigns.kitchenunitconverter.ABOUTUS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是启动活动的 Java:
package com.peakmobiledesigns.kitchenunitconverter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
protected int _splashTime = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while ((waited < _splashTime)) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
} finally {
finish();
startActivity(new Intent(getApplicationContext(),
KitchenConvertor.class));
stop();
}
}
};
splashTread.start();
}
}
My app crashes when I use use a menu button to switch to a new page and call a new Activity. I have programmed the same menu button/Activity in a few apps and never had a problem.
I have the following classes in my app:
- SplashActivity.java
- MainActivity.java
- AboutUs.Java
I have tested the menu button with reopening the SplashActivity and it worked, so the error is not with the menu button and calling the function. I have also changed the contents of the AboutUs.java to have nothing but the imports and the R.id.about_us xml and it still didn't work which makes me think there were no errors within the page.
My LogCat Error:
ERROR/AndroidRuntime(667):
ERROR: thread attach failed
08-04 12:07:13.039:
ERROR/AndroidRuntime(675):
ERROR: thread attach failed
08-04 12:07:19.119:
ERROR/gralloc(68): [unregister] handle 0x3ea8d8 still locked (state=40000001)
08-04 12:07:23.489:
ERROR/global(685): Deprecated Thread methods are not supported.
08-04 12:07:23.489:
ERROR/global(685): java.lang.UnsupportedOperationException
08-04 12:07:23.489:
ERROR/global(685): at java.lang.VMThread.stop(VMThread.java:85)
08-04 12:07:23.489:
ERROR/global(685): at java.lang.Thread.stop(Thread.java:1379)
08-04 12:07:23.489:
ERROR/global(685): at java.lang.Thread.stop(Thread.java:1344)
08-04 12:07:23.489:
ERROR/global(685): at com.peakmobiledesigns.kitchenunitconverter.SplashActivity$1.run(SplashActivity.java:28)
The menu java within the MainActivity that calls the AboutUs.java:
//menu starts here
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case R.id.feedback:
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]"[email protected]"});
startActivity(intent);
return true;
case R.id.about_us:
startActivity(new Intent("com.peakmobiledesigns.kitchenunitconverter.ABOUTUS"));
return true;
}
return false;
}
//menu ends here
And this is what I have listed for that activity in the Android Manifest:
<activity android:name=".AboutUs" android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.peakmobiledesigns.kitchenunitconverter.ABOUTUS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Here is the Java for the splash activity:
package com.peakmobiledesigns.kitchenunitconverter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
protected int _splashTime = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while ((waited < _splashTime)) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
} finally {
finish();
startActivity(new Intent(getApplicationContext(),
KitchenConvertor.class));
stop();
}
}
};
splashTread.start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您发布的代码,您正在调用 Thread.stop() ,这是一个已弃用的方法,Android 没有实现它。
在这种情况下,您根本不需要调用
stop
。当run
方法中的代码块完成时,Thread
将自行停止。删除对stop
的调用,您的异常就会消失。如果您确实需要在执行过程中停止
Thread
,则应该使用interrupt
方法让Thread知道它应该停止运行。您的Thread
代码需要调用isInterrupted
方法来了解是否停止运行。According to the code you posted, you're calling
Thread.stop()
which is a deprecated method and Android does not implement it.In this case, there's no need for you to call
stop
at all. AThread
will stop itself when the block of code in therun
method finishes. Remove the call tostop
and your exception will go away.If you do need to stop a
Thread
in the middle of its execution, you should instead use theinterrupt
method to let the Thread know that it should stop running. YourThread
code will need to call theisInterrupted
method to know whether to stop running.我发现了我的错误。在 Android Manifest 上,我将标识“AboutUs”页面的代码块放在应用程序后台之外。奇怪的是,在盯着代码几天之后,你终于发现了一个小错误。
感谢大家的帮助。
I found out my mistake. On the Android Manifest, I had put the chunk of code identifying my AboutUs page outside of the application backets. It's weird how after days of staring at code, you finally see the one tiny mistake.
Thanks for everyone's help.