2017-01-21 9 views
0

私は、クリック可能な部分を持つウィジェットを作成しました。これは、1つの詳細ではなくすべての詳細を示すアクティビティを開きます。しかし何か奇妙なことが起こるAndroid Widget ButtonがNullPointerExceptionをスローする

説明します。ウィジェットは完璧に動作しますが、ウィジェットを作成するとボタンは反応しません。 別のウィジェットを作成すると、合計で2つのウィジェットが作成されます。今、ボタンの種類の作品...それは私のクリックに応答しますが、クラッシュします。 私の電話機を完全に再起動してウィジェットのボタンを使用すると、それは正常に開きますか? 、

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: it.bjarn.android.myapp, PID: 2764 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{it.bjarn.android.myapp/it.bjarn.android.myapp.activities.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6119) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference 
         at it.bjarn.android.myapp.activities.DetailsActivity.onCreate(DetailsActivity.java:111) 
         at android.app.Activity.performCreate(Activity.java:6679) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
         at android.app.ActivityThread.-wrap12(ActivityThread.java)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:154)  
         at android.app.ActivityThread.main(ActivityThread.java:6119)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

をまた知っていると便利::これはにNullPointerExceptionユーザーが受信されている

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    for (int appWidgetId : appWidgetIds) { 
     updateAppWidget(context, appWidgetManager, appWidgetId); 

     Intent statsIntent = new Intent(context, DetailsActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("name", WidgetConfigureActivity.loadTitle(context, appWidgetId)); 
     bundle.putString("id", WidgetConfigureActivity.Id(context, appWidgetId)); 
     bundle.putString("avatarurl", WidgetConfigureActivity.loadAvatar(context, appWidgetId)); 
     statsIntent.putExtras(bundle); 

     PendingIntent statsPendingIntent = PendingIntent.getActivity(context, appWidgetId, statsIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 

     views.setOnClickPendingIntent(R.id.openInAppButton, statsPendingIntent); 

     ComponentName componentName = new ComponentName(context.getPackageName(), WidgetProvider.class.getName()); 

     appWidgetManager.updateAppWidget(componentName, views); 
    } 
} 

:ここ

は私にonUpdate()だそれが動作すると、それが唯一の変数との活動をオープンあなたがクリックしたウィジェットの代わりに1ウィジェットから。常に同じものを開きます。

//編集:

リクエストによってonCreate()クラスが追加されました。これは、別のアクティビティのボタンをクリックしてDetailsActivityを開くときに機能するため、ウィジェットに問題があります。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     preferences = PreferenceManager.getDefaultSharedPreferences(this); 
     Methods.setLanguage(this, preferences.getString("app_language", Locale.getDefault().getLanguage())); 
     setContentView(R.layout.activity_details); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     window = getWindow(); 

     mContext = this; 
     mApplication = getApplication(); 

     int mIntervalMultiply = Integer.parseInt(preferences.getString("app_sync_frequency", "5")); 
     mInterval = 1000 * mIntervalMultiply; 

     Bundle bundle = getIntent().getExtras(); 
     CharSequence title = bundle.getString("name"); 
     CharSequence id = bundle.getString("id"); 
     String avatar = bundle.getString("avatarurl"); 

     mChannelTitle = title != null ? title.toString() : null; 
     mChannelId = id != null ? id.toString() : null; 
     if (avatar != null) mChannelAvatar = avatar; 

     if (mChannelTitle.isEmpty() || mChannelId.isEmpty()) { 
      Intent search = new Intent(this, DetailsActivity.class); 
      startActivity(search); 
      Toast.makeText(this, R.string.error_connection_incorrect_response, Toast.LENGTH_LONG).show(); 
     } 
     this.setTitle(title); 

     mDbHelper = new DBHelper(this); 

     //noinspection ConstantConditions 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mDescriptionView = (TextView) findViewById(R.id.descriptionContent); 
     mCountOldView = (TextView) findViewById(R.id.oldCount); 
     mTotalViewsView = (TextView) findViewById(R.id.viewsCount); 
     mAvatarView = (ImageView) findViewById(R.id.avatarView); 
     mBannerView = (ImageView) findViewById(R.id.banner); 
     mCountView = (TickerView) findViewById(R.id.count); 


     mHandler = new Handler(); 

     getInitialData(this); 

     final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     if (mDbHelper.isInFavorites(mChannelId)) { 
      fab.setImageResource(R.drawable.ic_star_amber_a400_24dp); 
     } else { 
      fab.setImageResource(R.drawable.ic_star_grey_400_24dp); 
     } 
    } 
+0

(バンドルは、上記の例ではnullのときにアプリをクラッシュ何はおそらく

CharSequence title = bundle.getString("name"); 

です)。 DetailsActivity.classの – greysqrl

+0

? greysqrl –

+0

はいBjarnしてください。 – greysqrl

答えて

0

ログファイルによると、問題はonCreateのnullオブジェクト参照でgetStringを呼び出していることです。あなたのクラッシュの時点で、環境設定がnullであると思われます。あなたのコードの111行ですか?原因:java.lang.NullPointerException:it.bjarn.android.myapp.activitiesのnullオブジェクト参照で仮想メソッド 'java.lang.String android.os.BaseBundle.getString(java.lang.String)'を呼び出そうとしています.DetailsActivity.onCreate(D etailsActivity.java:111)。コールを分離し、プリファレンスがあるかどうかを確認します。私は 'getString'を呼び出す前に、else文にログを入れ、それが答えを検証します。 getSharedPreferencesを呼び出すときにコンテキストが有効であることを確認してください。 (無効なフォーマットについては申し訳ありませんが、私は携帯電話で応答しています)

+0

悲しいことに、オブジェクトのどれもヌルではなく、コンテキストが存在し、プリファレンスと同じです。どちらも定義されています。 –

0

アクティビティのgetIntent()はアクティビティを作成したインテントを返します。ウィジェットを押したときにアクティビティがすでに作成されている場合は、そのアクティビティを作成した元のインテントを取得します。これは、以前のウィジェットからのエキストラをクリックしたか、まったく持っていない可能性があります。

アクティビティの作成とインテントの処理を分離する必要があります。 Activity.onNewIntent()コールバックを参照してください。あなたののonCreate機能をしてください共有する必要が

+0

私は同じ意図を常に開いた問題を修正しましたが、それでもクラッシュし続けます。しかし、私は強制的にアプリを停止すると、それは動作します。 –

+0

もちろん、アプリを強制停止したときに動作します。これは、アクティビティが破棄され、意図が新しいアクティビティを作成しているためです。さもなければ、 'Activity.getIntent()'は古い意図を返します(これは過去にアクティビティの作成につながっています) –

+0

私は最初に1つのウィジェットを作成します。私のクリックに反応していません。 2番目のウィジェットを作成するとそれは応答しますが、クラッシュします。私が今アプリケーションを強制終了すると、完全に機能します。'intent.setData(Uri.parse(" tel:/ "+(int)System.currentTimeMillis())); –

関連する問題