1

我正在尝试为我的应用程序创建一个小部件。I want it to be like this : The user selects my widget among all widgets then a listview appears the user selects on of the items and the widget in that category (which is introduced in the application) will show up But when choosing the Widget " forceClose”发生。我附上我的代码谢谢你的帮助

public class WidgetConfig extends ListActivity implements OnItemClickListener { 

  AppWidgetManager awm;
  Context c;
  int awID;
  public String LS [] = {"C", "B", "A"}; 

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //  setContentView(R.id.listviewfirstshow);
    setListAdapter(new ArrayAdapter<String>(this,R.layout.widget_config , LS));
    ListView lv = (ListView)findViewById(R.id.listviewfirstshow);

    lv.setOnItemClickListener(this);
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    if (extras != null) {
      awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, 
                           AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    else {
      finish ();
    }

    awm = AppWidgetManager.getInstance(c);
  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    RemoteViews rv = new RemoteViews(c.getPackageName() R.layout.widget);
    Intent in =  new Intent(c, WidgetLearn2Activity.class);
    PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0);
    rv.setOnClickPendingIntent(arg2, pi);
    awm.updateAppWidget(awID, rv);

    Intent result = new Intent();
    result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
    setResult(RESULT_OK, result);
    finish();

  }
}

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="learn.widget" android:versionCode="1" android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="8" />
  <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
    <activity android:name=".WidgetLearn2Activity" android:label="@string/app_name" >
      <intent-filter>  
        <action android:name="android.intent.action.MAIN" />   
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter>
    </activity>
    <receiver android:name=".PointlessWidget" >
      <intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
      </intent-filter>
      <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_stuff" />
    </receiver>
    <activity android:name=".WidgetConfig" android:label="@string/app_name">
      <intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
      </intent-filter>
    </activity>
  </application>
</manifest>
4

0 回答 0