[英]Android don't launch default activity (MainActivity)
I have a very simple widget application with one single activity (MainActivity). 我有一个非常简单的窗口小部件应用程序,其中只有一个活动(MainActivity)。 At the very beginning I've created MainActivity from BlankActivity template.
从一开始,我就从BlankActivity模板创建了MainActivity。 I filled it a bit later with components and I've added it to AndroidManifest.
稍后我用组件填充了它,并将其添加到AndroidManifest中。
The problem is that Android don't launch the activity (the only one). 问题是Android不会启动活动 (唯一的活动 )。
What I see when app started is white blank view (with topbar with app-name). 应用启动时我看到的是白色空白视图(带有带有应用名称的顶部栏)。
I've put a breakpoint and didn't get to it during debug. 我已经设置了一个断点,并且在调试过程中没有到达断点。
I've tried to set DEFAULT category to MainActivity and without it: 我试图将DEFAULT类别设置为MainActivity,并且没有它:
<category android:name="android.intent.category.DEFAULT"/>
I've tried to put full or short path to Main Activity: 我尝试将主要活动的完整路径或简短路径:
android:name="pl.test.firstwidget.view.MainActivity"
android:name=".view.MainActivity"
I'm sure my MainActivity is in view package. 我确定我的MainActivity在视图包中。
AndroidManifest.xml: AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.test.firstwidget"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".view.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".view.MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" />
</receiver>
</application>
</manifest>
MainActivity.java: MainActivity.java:
package pl.test.firstwidget.view;
(...)
public class MainActivity extends AppCompatActivity {
@BindView(R.id.ordersNumber) TextView ordersNumber;
@BindView(R.id.ordersValue) TextView ordersValue;
@BindView(R.id.lastUpdate) TextView lastUpdate;
@BindView(R.id.refreshButton) TextView refreshButton;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fillView();
}
private void fillView() {
//empty so far
}
@OnClick(R.id.refreshButton)
public void refresh() {
//empty so far
}
}
build.gradle build.gradle
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId 'pl.test.firstwidget'
minSdkVersion 15
targetSdkVersion 25
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
compile group: 'joda-time', name: 'joda-time', version: '2.9.9'
compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
Does anybody see what I do wrong? 有人看到我做错了吗?
You need to override public void onCreate(Bundle savedInstanceState)
not public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) which is intended for api 21+ to persist your activity state across restarts 您需要覆盖
public void onCreate(Bundle savedInstanceState)
而不是public void onCreate(Bundle savedInstanceState,PersistableBundlepersistentState) ,这是为了让API 21+在重新启动后保持活动状态
Note Same as onCreate(android.os.Bundle) but called for those activities created with the attribute persistableMode set to persistAcrossReboots (in the manifest otherwise it won't get called hence empty view)
注意与onCreate(android.os.Bundle)相同,但是调用了那些将属性persistableMode设置为persistentAcrossReboots创建的活动(在清单中否则将不被调用,因此为空视图)
About PersistableBundle 关于PersistableBundle
if the activity is being re-initialized after previously being shut down or powered off then this Bundle contains the data it most recently supplied to outPersistentState in onSaveInstanceState(Bundle).
如果活动在先前关闭或关闭电源后正在重新初始化,则此捆绑包将其最近提供给onSaveInstanceState(Bundle)中的outPersistentState的数据。 Note: Otherwise it is null.
注意:否则为null。
So use 所以用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fillView();
}
It seems nothing is wrong. 看来没有错。
You don't need android.intent.category.DEFAULT
in the manifest, just android.intent.action.MAIN
and android.intent.category.LAUNCHER
should work 你不需要
android.intent.category.DEFAULT
在清单,只是android.intent.action.MAIN
和android.intent.category.LAUNCHER
应该工作
If you are starting Android development, for now, remove all references to butterKnife and don't use it yet; 如果您现在开始Android开发,请删除所有关于butterKnife的引用,并且不要使用它。 just to verify if it runs.
只是为了验证它是否运行。
change your onCreate
method as Pavneet_Singh answered, but don't use that super just use: 更改您的
onCreate
方法,如Pavneet_Singh回答,但不要使用该超级方法,只需使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.activity_main);
// ..code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.