-3

I am trying to make a tasker plugin, and all seems to be good until I try to configure the plugin. Here is the different code:

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>

Here is the 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) {

}

}

Here is the 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);
        }
    }
}

Here is the logcat of the error:

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
Nick Mowen
  • 2,572
  • 2
  • 22
  • 38

1 Answers1

2

You must call setContentView() before you can call findViewById(). Otherwise, like in your code, the findViewById() returned always null. Normally you call setContentView() directly after the super.onCreate().

For logcat understanding. This is your important part:

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)

This mean that you have an Object which is null (NullPointerException). The next line says that you call setAdapter() on a null Object which is located at your EditActivity in the onCreate() method on line 66.

StefMa
  • 3,344
  • 4
  • 27
  • 48