设置的Android设置和弹出菜单

时间:2013-05-07 15:29:05

标签: android

我正在处理我的应用中的设置。但是,当我尝试调用该设置时,我收到以下错误。 我正在使用滑动菜单和ABS。从以下错误看,它干扰我的设置被正确调用。谁能告诉这个日志导致它崩溃的原因是什么?

05-07 15:22:36.792: W/dalvikvm(5650): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-07 15:22:36.822: E/AndroidRuntime(5650): FATAL EXCEPTION: main
05-07 15:22:36.822: E/AndroidRuntime(5650): java.lang.NullPointerException
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.projectcaruso.naturalfamilyplaning.FragmentChange.showUserSettings(FragmentChange.java:130)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.projectcaruso.naturalfamilyplaning.FragmentChange.onOptionsItemSelected(FragmentChange.java:100)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:118)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:93)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:205)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.os.Handler.handleCallback(Handler.java:725)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.os.Looper.loop(Looper.java:137)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 15:22:36.822: E/AndroidRuntime(5650):     at dalvik.system.NativeStart.main(Native Method)

这是我的java:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.projectcaruso:
        Util.goToGitHub(this);
        return true;
    case R.id.about:
        new AlertDialog.Builder(this)
        .setTitle(R.string.about)
        .setMessage(Html.fromHtml(getString(R.string.about_msg)))
        .show();
        break;
    case R.id.licenses:
        new AlertDialog.Builder(this)
        .setTitle(R.string.licenses)
        .setMessage(Html.fromHtml(getString(R.string.license_detail)))
        .show();
        break;
    case R.id.contact:
        final Intent email = new Intent(android.content.Intent.ACTION_SENDTO);
        String uriText = "mailto:jfeinstein10@gmail.com" +
                "?subject=" + URLEncoder.encode("SlidingMenu Demos Feedback"); 
        email.setData(Uri.parse(uriText));
        try {
            startActivity(email);
        } catch (Exception e) {
            Toast.makeText(this, R.string.no_email, Toast.LENGTH_SHORT).show();
        }
        break;
    case R.id.settings:
        showUserSettings();
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.settings, menu);
    return true;
}


private void showUserSettings() {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);

    StringBuilder builder = new StringBuilder();

    builder.append("\n Username: "
            + sharedPrefs.getString("prefUsername", "NULL"));

    builder.append("\n Send report:"
            + sharedPrefs.getBoolean("prefSendReport", false));

    builder.append("\n Sync Frequency: "
            + sharedPrefs.getString("prefSyncFrequency", "NULL"));

    TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);

    settingsTextView.setText(builder.toString());
}

编辑XML:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
        <item 
            android:id="@+id/projectcaruso" 
            android:icon="@drawable/ic_action_github"
            android:showAsAction="always" 
            android:title="@string/projectcaruso_label" 
            android:titleCondensed="@string/projectcaruso" 
            android:visible="true">
        </item>
        <item 
            android:id="@+id/about" 
            android:title="@string/about" 
            android:showAsAction="never" 
            android:orderInCategory="1">
        </item>
        <item 
            android:id="@+id/licenses" 
            android:title="@string/licenses" 
            android:showAsAction="never" 
            android:orderInCategory="2">
        </item>
        <item 
            android:id="@+id/contact" 
            android:title="@string/contact" 
            android:orderInCategory="3" 
            android:showAsAction="never">
        </item>
            <item 
            android:id="@+id/settings" 
            android:title="@string/settings" 
            android:orderInCategory="3" 
            android:showAsAction="never">
        </item>
    </menu>

1 个答案:

答案 0 :(得分:0)

您的settingsTextViewnull

尝试像这样引用TextView

TextView settingsTextView = (TextView) getActivity().findViewById(R.id.textUserSettings);