大家好(对此感到抱歉),我正在编写一个代码,该代码允许我在应用小部件的设置屏幕中输入一个数字。然后我们将其保存并存储为一个变量,以在为其制作小部件的主应用程序中使用。我想知道如何捆绑选择的号码并将其传输到要运行的应用程序/小部件。
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.TextView;
public class WWSettings extends Activity implements OnClickListener {
Context context = this;
Button svBtn;
AppWidgetManager awm;
EditText userNum;
TextView unOut;
int awID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
svBtn = (Button) findViewById(R.id.btnwidgetconfig);
emsNum = (EditText) findViewById(R.id.etwidgitconfig);
emsOut = (TextView) findViewById(R.id.unOut);
svBtn.setOnClickListener(this);
//get widget info!
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(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String e = userNum.getText().toString();
RemoteViews view = new RemoteViews(this.getPackageName(), R.layout.widget);
view.setTextViewText(R.id.unOut, "Your Number is set to" + e);
Intent sparkintent = new Intent(this, startSpark.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, sparkintent, 0);
view.setOnClickPendingIntent(R.id.btnwidgetconfig, pi);
sparkintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
awm.updateAppWidget(awID, view);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
}
logz 的 Das Kat:
07-26 17:13:08.629: W/dalvikvm(1488): threadid=1: thread exiting with uncaught exception (group=0x40018560)
07-26 17:13:08.639: E/AndroidRuntime(1488): FATAL EXCEPTION: main
07-26 17:13:08.639: E/AndroidRuntime(1488): java.lang.RuntimeException: Unable to start activity ComponentInfo{lionsimage.com.this/lionsimage.com.this.startSpark}: java.lang.NullPointerException
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.os.Looper.loop(Looper.java:130)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-26 17:13:08.639: E/AndroidRuntime(1488): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 17:13:08.639: E/AndroidRuntime(1488): at java.lang.reflect.Method.invoke(Method.java:507)
07-26 17:13:08.639: E/AndroidRuntime(1488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
07-26 17:13:08.639: E/AndroidRuntime(1488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
07-26 17:13:08.639: E/AndroidRuntime(1488): at dalvik.system.NativeStart.main(Native Method)
07-26 17:13:08.639: E/AndroidRuntime(1488): Caused by: java.lang.NullPointerException
07-26 17:13:08.639: E/AndroidRuntime(1488): at lionsimage.com.webb.startSpark.onCreate(startSpark.java:28)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 17:13:08.639: E/AndroidRuntime(1488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-26 17:13:08.639: E/AndroidRuntime(1488): ... 11 more