2016-07-17 24 views
0

AndroidStudioで新しいアンドロイドプロジェクトを作成し、アプリウィジェットを作成し、設定アクティビティのオプションを選択します。 AndroidStudioはプロバイダ情報xmlを生成し、マニフェストXMLのdeclerationsと両方のJavaクラスを生成します。 1つのアクティビティ、1つのウィジェットプロバイダ。アプリウィジェットを実行する - 起動アクティビティを特定できませんでした

これは実行可能ですが、エラーが発生します。起動アクティビティを特定できませんでした:デフォルトのアクティビティが見つかりませんでした。アクティビティの起動中にエラーが発生しました。打ち上げフィールドには赤い十字が表示されます。

なぜデフォルト動作がないので、わかりません。ウィジェットプロバイダが動作を開始すると、設定アクティビティが開始されます。これを行うには、android.appwidget.action.APPWIDGET_CONFIGUREを使用してアクティビティのインテントフィルタがあります。

インテントフィルターLAUNCHERとDEFAULTにもカテゴリを追加します。プロバイダーと活動中。しかし、まだエラーメッセージが表示されます。

起動設定で「Nothing」を選択してアプリを実行すると、多くのエラーメッセージしか表示されません。アプリケーションがオンラインになるのを待っています:com.example.desktop_win10.myapplication | com.example.desktop_win10.myapplication.test

しかし、ウィジェットはインストールされておらず、実行されません。何が間違っているの?私はIntellijとAndroidStudioを試してみます。

たManifest.xml

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <receiver android:name=".NewAppWidget" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/new_app_widget_info" /> 
     </receiver> 

     <activity android:name=".NewAppWidgetConfigureActivity" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

widget_info.xml:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="40dp" 
    android:minHeight="40dp" 
    android:updatePeriodMillis="86400000" 
    android:previewImage="@drawable/example_appwidget_preview" 
    android:initialLayout="@layout/new_app_widget" 
    android:configure="com.example.desktop_win10.myapplication.NewAppWidgetConfigureActivity" 
    android:resizeMode="horizontal|vertical" 
    android:widgetCategory="home_screen" 
    android:initialKeyguardLayout="@layout/new_app_widget"> 
</appwidget-provider> 

そして、2つのJavaクラスがあります。



EDIT1:今、私は追加

カテゴリLAUNCHERとアクションMAIN:

<activity android:name=".NewAppWidgetConfigureActivity" > 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
     <action android:name="android.intent.action.MAIN"/> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

私は時々、デバッガウィジェットウィジェットストア内イスト、時にはないを開始した場合。

私はまた、生成されたJavaアクティビティクラスで呼び出さ仕上げ方法であることを参照してください。

// If this activity was started with an intent without an app widget ID, finish with an error. 
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { 
    finish(); 
    return; 
} 

しかし、私はTHIDを削除した場合、それはまた仕事をdoesntの。なぜ私はデフォルトのGoogleの例が動作しないのか分かりません。ここで

はJavaクラスです:

活動:

package com.example.desktop_win10.myapplication; 

import android.app.Activity; 
import android.appwidget.AppWidgetManager; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 

/** 
* The configuration screen for the {@link NewAppWidget NewAppWidget} AppWidget. 
*/ 
public class NewAppWidgetConfigureActivity extends Activity { 

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; 
    EditText mAppWidgetText; 
    private static final String PREFS_NAME = "com.example.desktop_win10.myapplication.NewAppWidget"; 
    private static final String PREF_PREFIX_KEY = "appwidget_"; 

    public NewAppWidgetConfigureActivity() { 
     super(); 
    } 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     // Set the result to CANCELED. This will cause the widget host to cancel 
     // out of the widget placement if the user presses the back button. 
     setResult(RESULT_CANCELED); 

     setContentView(R.layout.new_app_widget_configure); 
     mAppWidgetText = (EditText)findViewById(R.id.appwidget_text); 
     findViewById(R.id.add_button).setOnClickListener(mOnClickListener); 

     // Find the widget id from the intent. 
     Intent intent = getIntent(); 
     Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      mAppWidgetId = extras.getInt(
        AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
     } 

     // If this activity was started with an intent without an app widget ID, finish with an error. 
     if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { 
      finish(); 
      return; 
     } 

     mAppWidgetText.setText(loadTitlePref(NewAppWidgetConfigureActivity.this, mAppWidgetId)); 
    } 

    View.OnClickListener mOnClickListener = new View.OnClickListener() { 
     public void onClick(View v) { 
      final Context context = NewAppWidgetConfigureActivity.this; 

      // When the button is clicked, store the string locally 
      String widgetText = mAppWidgetText.getText().toString(); 
      saveTitlePref(context,mAppWidgetId,widgetText); 

      // It is the responsibility of the configuration activity to update the app widget 
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
      NewAppWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId); 

      // Make sure we pass back the original appWidgetId 
      Intent resultValue = new Intent(); 
      resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
      setResult(RESULT_OK, resultValue); 
      finish(); 
     } 
    }; 

    // Write the prefix to the SharedPreferences object for this widget 
    static void saveTitlePref(Context context, int appWidgetId, String text) { 
     SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); 
     prefs.putString(PREF_PREFIX_KEY + appWidgetId, text); 
     prefs.apply(); 
    } 

    // Read the prefix from the SharedPreferences object for this widget. 
    // If there is no preference saved, get the default from a resource 
    static String loadTitlePref(Context context, int appWidgetId) { 
     SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); 
     String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); 
     if (titleValue != null) { 
      return titleValue; 
     } else { 
      return context.getString(R.string.appwidget_text); 
     } 
    } 

    static void deleteTitlePref(Context context, int appWidgetId) { 
     SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); 
     prefs.remove(PREF_PREFIX_KEY + appWidgetId); 
     prefs.apply(); 
    } 
} 

とプロバイダ:

package com.example.desktop_win10.myapplication; 

import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.widget.RemoteViews; 

/** 
* Implementation of App Widget functionality. 
* App Widget Configuration implemented in {@link NewAppWidgetConfigureActivity NewAppWidgetConfigureActivity} 
*/ 
public class NewAppWidget extends AppWidgetProvider { 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     // There may be multiple widgets active, so update all of them 
     for (int appWidgetId : appWidgetIds) { 
      updateAppWidget(context, appWidgetManager, appWidgetId); 
     } 
    } 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
     // When the user deletes the widget, delete the preference associated with it. 
     for (int appWidgetId : appWidgetIds) { 
      NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId); 
     } 
    } 

    @Override 
    public void onEnabled(Context context) { 
     // Enter relevant functionality for when the first widget is created 
    } 

    @Override 
    public void onDisabled(Context context) { 
     // Enter relevant functionality for when the last widget is disabled 
    } 

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
      int appWidgetId) { 

     CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId); 
     // Construct the RemoteViews object 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); 
     views.setTextViewText(R.id.appwidget_text, widgetText); 

     // Instruct the widget manager to update the widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
} 
+0

'デフォルトのアクティビティが見つかりませんでした.'は非常に明確に問題です。デフォルトのインテントを追加すると、エラーは何ですか?また、あなたの時間を無駄にして申し訳ありませんが、IDEの切り替えでコードのコンパイル方法は変わりません –

+0

@ cricket_007問題は、アクティビティのないウィジェットプロジェクトなので、デフォルトのアクティビティはありません。プロバイダは、アクションintent-filter android.appwidget.action.APPWIDGET_CONFIGUREによって唯一(設定アクティビティ)を開始する必要があります。 – gegengift

答えて

0

は、あなたが追加しました:

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

私が知っているように、それなしではアンドロイドはどのアクティビティをメインアクティビティとして起動するのかわかりません。

+0

正確ではありません。しかし、今私はプロバイダのdoesnの仕事を試してみてください。起動設定で「何も」を選択できないのはなぜですか?また、構成アクティビティを開始する必要があるandroid.appwidget.action.APPWIDGET_CONFIGUREを宣言しているため、アクティビティが誤っている可能性があります。今私は活動でそれを試してみましょう:3つの試行後、これまで(それはまれに動作)動作します。 – gegengift

+0

アプリケーションはインストールされましたが、コンソールに次のように表示されます。アプリケーションがオンラインになるのを待っています。簡単なショー:デバッガを待っています...力..?アクティビティの開始と終了を少し待っています。アプリケーションがインストールされましたが、ウィジェットはインストールされていません。私は理由を知っていませんが、今度は4つの試行の後、ウィジェットはウィジェットメニューにあります。しかし、私はそれをホームスクリーンに追加すると、デバッガはそれを登録しません。 – gegengift

+0

「キャッシュの無効化とクリア」を試しましたか? –

関連する問題