我正在尝试创建一个任务器插件,在我尝试配置插件之前,一切似乎都很好。这是不同的代码:
edit_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
>
</Spinner>
</LinearLayout>
这是edit_activity.java
/**
* This is the "Edit" activity for a Locale Plug-in.
* <p/>
* This Activity can be started in one of two states:
* <ul>
* <li>New plug-in instance: The Activity's Intent will not contain
* {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE}.</li>
* <li>Old plug-in instance: The Activity's Intent will contain
* {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} from a previously saved plug-in instance that the
* user is editing.</li>
* </ul>
*
* @see com.twofortyfouram.locale.Intent#ACTION_EDIT_SETTING
* @see com.twofortyfouram.locale.Intent#EXTRA_BUNDLE
*/
public final class EditActivity extends AbstractPluginActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1;
public void setSound (String Mode){
if (Mode == "Normal") {AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);}
else if (Mode == "Vibrate") {AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);}
else {AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner1= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.Modes,android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
BundleScrubber.scrub(getIntent());
final Bundle localeBundle = getIntent().getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
BundleScrubber.scrub(localeBundle);
setContentView(R.layout.edit_activity);
if (null == savedInstanceState) {
if (PluginBundleManager.isBundleValid(localeBundle)) {
final String message =
localeBundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
((EditText) findViewById(android.R.id.text1)).setText(message);
}
}
}
@Override
public void finish() {
//User cancelled
if (isCanceled()) {
super.finish();
return;
}
//Sanity check
final String message = ((Spinner) findViewById(android.R.id.text1)).getSelectedItem().toString();
if (message.length() <= 0) {
super.finish();
return;
}
final Intent resultIntent = new Intent();
/*
* This extra is the data to ourselves: either for the Activity or the BroadcastReceiver. Note
* that anything placed in this Bundle must be available to Locale's class loader. So storing
* String, int, and other standard objects will work just fine. Parcelable objects are not
* acceptable, unless they also implement Serializable. Serializable objects must be standard
* Android platform objects (A Serializable class private to this plug-in's APK cannot be
* stored in the Bundle, as Locale's classloader will not recognize it).
*/
final Bundle resultBundle = PluginBundleManager.generateBundle(getApplicationContext(), message);
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, resultBundle);
/*
* The blurb is concise status text to be displayed in the host's UI (Tasker UI itself).
*/
final String blurb = generateBlurb(getApplicationContext(), message);
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_STRING_BLURB, blurb);
setResult(RESULT_OK, resultIntent);
super.finish();
}
/**
* @param context Application context.
* @param message The toast message to be displayed by the plug-in. Cannot be null.
* @return A blurb for the plug-in.
*/
/* package */
static String generateBlurb(final Context context, final String message) {
final int maxBlurbLength = context.getResources().getInteger(R.integer.twofortyfouram_locale_maximum_blurb_length);
String finalMessage = message;
if (finalMessage.length() > maxBlurbLength)
finalMessage = finalMessage.substring(0, maxBlurbLength);
return finalMessage;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
Toast.makeText(this,"You Selected "+myText.getText(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
这是FireReceiver.java
public final class FireReceiver extends BroadcastReceiver
{
/**
* @param context {@inheritDoc}.
* @param intent the incoming {@link com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING} Intent. This
* should contain the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} that was saved by
* {@link EditActivity} and later broadcast by Locale.
*/
@Override
public void onReceive(final Context context, final Intent intent)
{
/*
* Always be strict on input parameters! A malicious third-party app could send a malformed Intent.
*/
if (!com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING.equals(intent.getAction()))
{
if (Constants.IS_LOGGABLE)
Log.e(Constants.LOG_TAG, String.format(Locale.US, "Received unexpected Intent action %s", intent.getAction())); //$NON-NLS-1$
return;
}
BundleScrubber.scrub(intent);
final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
BundleScrubber.scrub(bundle);
if (PluginBundleManager.isBundleValid(bundle))
{
final String message = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
EditActivity eta= new EditActivity();
eta.setSound(message);
}
}
}
这是错误的logcat:
Process: com.yourcompany.yoursetting, PID: 11660
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yourcompany.yoursetting/com.yourcompany.yoursetting.ui.EditActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
at android.app.ActivityThread.access$900(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.yourcompany.yoursetting.ui.EditActivity.onCreate(EditActivity.java:66)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879)
at android.app.ActivityThread.access$900(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194
答案 0 :(得分:2)
您必须先致电setContentView()
,然后才能致电findViewById()
。
否则,与您的代码一样,findViewById()
始终返回null
。
通常,您会在setContentView()
之后直接致电super.onCreate()
。
logcat
理解。这是你的重要部分:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
at com.yourcompany.yoursetting.ui.EditActivity.onCreate(EditActivity.java:66)
这意味着您有Object
null
(NullPointerException)。下一行表示您在位于第66行setAdapter()
方法null
的{{1}} Object
上致电EditActivity
。