2017-08-04 101 views
3

我已經創建完成後我自己的應用項目類:Android的工作室默認的活動

package com.example.bezzabarovaa.myapplication; 
import android.app.Application; 
import android.content.Context; 

/** 
* Created by bezzabarovaa on 04.08.2017. 
*/ 

public class app extends Application { 
public static Context context; 

@Override public void onCreate() { 
    super.onCreate(); 
    context = getApplicationContext(); 
} 

} 

,並更改的manifest.xml(應用程序改變到App類)

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bezzabarovaa.myapplication"> 

<app 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" 
     android:label = "@string/app_name"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 
</app> 

我已收到消息Error running Unnamed:Default Activity not found

如果我會改變e我的班級app返回到清單文件中的application - 一切正常。 問題在哪裏? 謝謝。

+0

你的活動在哪裏? – SripadRaj

+0

是什麼讓你覺得你可以改變標籤名稱,它不會破壞? –

+2

您無法更改標籤名稱!你應該使用'>'並且遵循Java指南,其中類名應該是CamelCase(不是應用程序,而是應用程序)。 – shimatai

回答

7

<app應該是<application您指定自定義應用程序在這裏:

<application android:name=".app" ... 

檢查documentation名節說:

用於實現應用程序子類的全名 該應用程序。當應用程序進程啓動時,該類 在應用程序的任何組件之前被實例化。 子類是可選的;大多數應用程序不需要一個。在子類的 不存在的情況下,Android使用基本Application類的一個實例。

完整的示例(與您的代碼):

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bezzabarovaa.myapplication"> 

<application 
    android:name=".app" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" 
     android:label = "@string/app_name"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 
0

添加下面一行在你menifest應用程序標記

<application 
    android:name=".app" 
    ... 
    </application> 
+2

這段代碼如何解決提問者的問題?他已經表示,如果他將其改回「」,他就知道它有效。這正是你在這裏推薦的。 –

1

使用下面的代碼片段

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bezzabarovaa.myapplication"> 

<application 
    android:name=".app" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity" 
     android:label = "@string/app_name"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 

     </intent-filter> 
    </activity> 
</application> 

希望這會對你有所幫助。

+0

這段代碼如何解決提問者的問題?他已經表示,如果他將其改回「」,他就知道它有效。這正是你在這裏推薦的。 –

+0

@CodyGray;提問者的代碼不包括'android:name =「.app」'部分,而這個(和另一個答案) – 0xDEADC0DE

1

不久前我面臨同樣的問題,在將默認活動更改爲另一個之後。

什麼解決它是無效的高速緩存和重新啓動的Android工作室:

File -> Invalidate Caches/Restart...

,然後點擊Invalidate and Restart

0

通過轉到「編輯配置」菜單(點擊「Shift」三次,輸入「編輯配置」),然後將啓動選項>啓動更改爲「無」,您可以讓Android Studio不會抱怨。

我可能會添加一個包含指令的通用活動,以避免混淆。

相關問題