簡體   English   中英

Android不啟動默認活動(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在視圖包中。

  • 我曾嘗試從模擬器中卸載應用程序,然后再次安裝應用程序,但這並沒有幫助。
  • 我試圖將應用程序安裝到另一個模擬器上-沒有成功
  • 我嘗試將應用程序安裝到手機-沒有成功。
  • 我試圖清理並重建我的項目-沒有成功。
  • 我試圖在AndroidStudio運行配置中設置DefaultActivity-失敗。
  • 我試圖在運行配置中設置指定的活動(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();
}

看來沒有錯。

  1. 你不需要android.intent.category.DEFAULT在清單,只是android.intent.action.MAINandroid.intent.category.LAUNCHER應該工作

  2. 如果您現在開始Android開發,請刪除所有關於butterKnife的引用,並且不要使用它。 只是為了驗證它是否運行。

  3. 更改您的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM