App crashes when switching to a new Activity/app page
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 开发者_StackOverflow社区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[]"peakmobiledesigns@gmail.com"});
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();
}
}
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. A Thread
will stop itself when the block of code in the run
method finishes. Remove the call to stop
and your exception will go away.
If you do need to stop a Thread
in the middle of its execution, you should instead use the interrupt
method to let the Thread know that it should stop running. Your Thread
code will need to call the isInterrupted
method to know whether to stop running.
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.
精彩评论