Cześć, tworze aplikację z widgetem na androida. Potrzebuję dynamicznie dodawać lub usuwać przyciski w widgecie.
Mam taki kod klasy Widget.java:
package com.fxteam.malcome;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Button;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.util.Log;
public class Widget extends AppWidgetProvider {
private static final String SYNC_CLICKED = "malcome_widget_button";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
Intent intent2 = new Intent(context, MainActivity.class);
intent2.putExtra("accountID", "0");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget);
views.setOnClickPendingIntent(R.id.malcome_widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Button myButton = new Button(context);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout); // <--- error here
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}
Ten kod powinien dodać przycisk:
Button myButton = new Button(context);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout); // <--- błąd
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Niestety dostaje taki błąd
cannot find symbol
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout);
^
symbol: method findViewById(int)
Wiecie może jak to naprawić?