2015-09-07 159 views
-3

我想製作一個tasker插件,並且所有內容似乎都很好,直到我嘗試配置該插件。下面是不同的代碼:Android Studio應用程序崩潰開始

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 
+4

有時(總是)最重要的部分是logcat的看是失敗 –

+3

安置自己的logcat的和檢查你的帖子。部分代碼不能很好地打包閱讀。 –

+0

這裏是logcat。 –

回答

2

必須調用setContentView()你可以打電話findViewById()之前。 否則,像你的代碼一樣,findViewById()總是返回null。 通常您在super.onCreate()之後直接致電setContentView()

對於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(空指針異常)。下一行說,你叫setAdapter()nullObject它位於您EditActivityonCreate()方法上線66