![](/img/trans.png)
[英]Is it possible that Android App will not launch the MainActivity?
[英]Android don't launch default activity (MainActivity)
我有一個非常簡單的窗口小部件應用程序,其中只有一個活動(MainActivity)。 從一開始,我就從BlankActivity模板創建了MainActivity。 稍后我用組件填充了它,並將其添加到AndroidManifest中。
問題是Android不會啟動活動 (唯一的活動 )。
應用啟動時我看到的是白色空白視圖(帶有帶有應用名稱的頂部欄)。
我已經設置了一個斷點,並且在調試過程中沒有到達斷點。
我試圖將DEFAULT類別設置為MainActivity,並且沒有它:
<category android:name="android.intent.category.DEFAULT"/>
我嘗試將主要活動的完整路徑或簡短路徑:
android:name="pl.test.firstwidget.view.MainActivity"
android:name=".view.MainActivity"
我確定我的MainActivity在視圖包中。
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:
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
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'
}
有人看到我做錯了嗎?
您需要覆蓋public void onCreate(Bundle savedInstanceState)
而不是public void onCreate(Bundle savedInstanceState,PersistableBundlepersistentState) ,這是為了讓API 21+在重新啟動后保持活動狀態
注意與onCreate(android.os.Bundle)相同,但是調用了那些將屬性persistableMode設置為persistentAcrossReboots創建的活動(在清單中否則將不被調用,因此為空視圖)
關於PersistableBundle
如果活動在先前關閉或關閉電源后正在重新初始化,則此捆綁包將其最近提供給onSaveInstanceState(Bundle)中的outPersistentState的數據。 注意:否則為null。
所以用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fillView();
}
看來沒有錯。
你不需要android.intent.category.DEFAULT
在清單,只是android.intent.action.MAIN
和android.intent.category.LAUNCHER
應該工作
如果您現在開始Android開發,請刪除所有關於butterKnife的引用,並且不要使用它。 只是為了驗證它是否運行。
更改您的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.