在设备启动时启动应用程序时出错
Error when launching app on device start
我正在构建一个需要在我的 android 设备首次启动时启动的应用程序,它工作正常。我正在解析一个 XML 文件并基于我从 XML 文件中获得的值我从我的主文件 activity 开始另一个 Activity 但是当我
开始尝试从我的 MainActivity onCreate 启动一个 Activity 我开始收到以下错误。
01-03 12:06:41.170 7854-7854/com.example.asiahyd.gpsdemo E/ActivityThread﹕ Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3344)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3425)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
这是我的 MainActivity onCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
check = getIntent().getExtras().getBoolean("startUp");
setContentView(R.layout.activity_my);
String baseDir = Environment.getExternalStorageDirectory().toString() + "/Automation";
File f = new File(baseDir);
int numOfFiles = f.listFiles().length;
File[] temp = f.listFiles();
try {
parseData(temp[0]);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
if(check == true){
Intent i = new Intent(getApplicationContext(), Result.class);
i.putExtra("coldSelected", coldSelected);
i.putExtra("warmSelected", warmSelected);
i.putExtra("hotSelected", hotSelected);
i.putExtra("onDeviceLogging", 0);
startActivity(i);
}
}
然后这是 activity 我用来在设备启动时启动我的应用程序
public class Bootup extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("startUp", true);
context.startActivity(i);
}
}
}
如果有人知道我为什么会收到此错误,我将非常感谢您的帮助
此处发现类似问题:Similar Issue on SO。解决方案似乎是将您的 Intent 添加到 onResume() 或 onPostResume()。这是由于高端手机上的应用 运行。
public void onResume(){
super.onResume();
//XML parse and intent here
}
或:
public void onPostResume(){
super.onPostResume();
//XML parse and intent here
}
这也是另一个可行的答案:
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 1:
//start activity with intent here
default:
break;
}
return false;
}
});
并在 onResume 中调用它。
handler.sendEmptyMessageDelayed(1, 1000);
旁注:我还想建议创建一个意图如下:
Intent i = new Intent(MainActivity.this, Result.class);
关于原因,我可以提供一个长篇大论的答案,但简而言之,除非您知道原因,否则不要使用 getApplicationContext() :)
我正在构建一个需要在我的 android 设备首次启动时启动的应用程序,它工作正常。我正在解析一个 XML 文件并基于我从 XML 文件中获得的值我从我的主文件 activity 开始另一个 Activity 但是当我 开始尝试从我的 MainActivity onCreate 启动一个 Activity 我开始收到以下错误。
01-03 12:06:41.170 7854-7854/com.example.asiahyd.gpsdemo E/ActivityThread﹕ Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.example.asiahyd.gpsdemo/com.example.asiahyd.gpsdemo.MyActivity}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3344)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3425)
at android.app.ActivityThread.access00(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
这是我的 MainActivity onCreate 方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
check = getIntent().getExtras().getBoolean("startUp");
setContentView(R.layout.activity_my);
String baseDir = Environment.getExternalStorageDirectory().toString() + "/Automation";
File f = new File(baseDir);
int numOfFiles = f.listFiles().length;
File[] temp = f.listFiles();
try {
parseData(temp[0]);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
if(check == true){
Intent i = new Intent(getApplicationContext(), Result.class);
i.putExtra("coldSelected", coldSelected);
i.putExtra("warmSelected", warmSelected);
i.putExtra("hotSelected", hotSelected);
i.putExtra("onDeviceLogging", 0);
startActivity(i);
}
}
然后这是 activity 我用来在设备启动时启动我的应用程序
public class Bootup extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("startUp", true);
context.startActivity(i);
}
}
}
如果有人知道我为什么会收到此错误,我将非常感谢您的帮助
此处发现类似问题:Similar Issue on SO。解决方案似乎是将您的 Intent 添加到 onResume() 或 onPostResume()。这是由于高端手机上的应用 运行。
public void onResume(){
super.onResume();
//XML parse and intent here
}
或:
public void onPostResume(){
super.onPostResume();
//XML parse and intent here
}
这也是另一个可行的答案:
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 1:
//start activity with intent here
default:
break;
}
return false;
}
});
并在 onResume 中调用它。
handler.sendEmptyMessageDelayed(1, 1000);
旁注:我还想建议创建一个意图如下:
Intent i = new Intent(MainActivity.this, Result.class);
关于原因,我可以提供一个长篇大论的答案,但简而言之,除非您知道原因,否则不要使用 getApplicationContext() :)