簡體   English   中英

無法以編程方式啟動DialogPreference(拋出空指針異常)

[英]Cannot launch DialogPreference programmatically (Null Pointer Exception thrown)

我有一個擴展DialogPreference的自定義類。 如果從“首選項”菜單啟動,它將完美運行。 我希望能夠從Activity中啟動它。 下面是我的DialogPreference類,其中我公開了此線程建議的showDialog()方法。 當我調用它時,我得到一個空指針異常,但無法找出原因。

在第27行拋出錯誤,它位於onBindDialogView()中,其中調用了hText.setText()。

package com.jumptuck.recipebrowser;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

// Pop-up dialog used to set and modify host and login credentials
public class HostCredentialsDialogPreference extends DialogPreference {
    static final String TAG = "HostCredentialsDialogPreference";
    EditText hText, uText, pText;

    public HostCredentialsDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.dialog_host_credentials);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        Log.d(TAG,"onBindDialogView");

        SharedPreferences sp = getSharedPreferences();
        hText.setText(sp.getString("host", ""));
        uText.setText(sp.getString("username", ""));
        pText.setText(sp.getString("password", ""));
    }

    @Override
    protected View onCreateDialogView() {
        // Guide for this technique found at:
        // http://alexfu.tumblr.com/post/23683149440/android-dev-custom-dialogpreference
        Log.d(TAG,"onCreateDialogView");
        View root = super.onCreateDialogView();
        hText = (EditText) root.findViewById(R.id.host);
        uText = (EditText) root.findViewById(R.id.username);
        pText = (EditText) root.findViewById(R.id.password);
        return root;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        if (positiveResult){
            Log.d(TAG,"Clicked Save");
            SharedPreferences sp = getSharedPreferences();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("host", hText.getText().toString());
            editor.putString("username", uText.getText().toString());
            editor.putString("password", pText.getText().toString());
            editor.commit();
        }
        else {
            Log.d(TAG,"Clicked Cancel");
        }
    }

    void show() {
        showDialog(null);
    }

}

我正在使用“測試”按鈕嘗試從另一個Activity啟動對話框:

public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(TAG, "onOptionsItemSelected");

    switch (item.getItemId()) {
    case R.id.item_prefs:
        startActivity(new Intent(this, PrefsActivity.class));
        return true;
    case R.id.refresh:
        if (credentialsExist()) {
            refreshListView();
        }
        return true;
    case R.id.recipe_dir:
        startActivity(new Intent(this, RecipeDisplayActivity.class));
        return true;
    case R.id.testing:
        HostCredentialsDialogPreference hc = new HostCredentialsDialogPreference(this, null);
        hc.show();
        return true;

    default:
        return false;
    }
}

以下是我的首選項和對話首選項的xml文件:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <com.jumptuck.recipebrowser.HostCredentialsDialogPreference
        android:key="dialog_credentials"
        android:title="Server Address and Login"
        android:summary="Set Host, Username and Password" />

</PreferenceScreen>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dialog_hostname_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dialog_hint_uri" />

    <EditText
        android:id="@+id/host"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textUri" />

    <TextView
        android:id="@+id/dialog_username_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dialog_hint_user" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <TextView
        android:id="@+id/dialog_password_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dialog_hint_password" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:inputType="textPassword" />

</LinearLayout>

最后是Logcat:

D/RecipeListActivity( 5894): onOptionsItemSelected
D/HostCredentialsDialogPreference( 5894): onCreateDialogView
D/HostCredentialsDialogPreference( 5894): onBindDialogView
D/AndroidRuntime( 5894): Shutting down VM
W/dalvikvm( 5894): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
E/AndroidRuntime( 5894): FATAL EXCEPTION: main
E/AndroidRuntime( 5894): java.lang.NullPointerException
E/AndroidRuntime( 5894):    at com.jumptuck.recipebrowser.HostCredentialsDialogPreference.onBindDialogView(HostCredentialsDialogPreference.java:27)
E/AndroidRuntime( 5894):    at android.preference.DialogPreference.showDialog(DialogPreference.java:289)
E/AndroidRuntime( 5894):    at com.jumptuck.recipebrowser.HostCredentialsDialogPreference.show(HostCredentialsDialogPreference.java:62)
E/AndroidRuntime( 5894):    at com.jumptuck.recipebrowser.RecipeListActivity.onOptionsItemSelected(RecipeListActivity.java:192)
E/AndroidRuntime( 5894):    at android.app.Activity.onMenuItemSelected(Activity.java:2534)
E/AndroidRuntime( 5894):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
E/AndroidRuntime( 5894):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
E/AndroidRuntime( 5894):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
E/AndroidRuntime( 5894):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
E/AndroidRuntime( 5894):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
E/AndroidRuntime( 5894):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
E/AndroidRuntime( 5894):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
E/AndroidRuntime( 5894):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
E/AndroidRuntime( 5894):    at android.widget.AbsListView$1.run(AbsListView.java:3533)
E/AndroidRuntime( 5894):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime( 5894):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 5894):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 5894):    at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 5894):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 5894):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 5894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 5894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 5894):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  148):   Force finishing activity com.jumptuck.recipebrowser/.RecipeListActivity
W/WindowManager(  148): Failure taking screenshot for (246x410) to layer 21025
W/ActivityManager(  148): Activity pause timeout for ActivityRecord{412097e0 com.jumptuck.recipebrowser/.RecipeListActivity}
I/Choreographer(  281): Skipped 39 frames!  The application may be doing too much work on its main thread.
W/ActivityManager(  148): Activity destroy timeout for ActivityRecord{412097e0 com.jumptuck.recipebrowser/.RecipeListActivity}

誰知道我做錯了什么? 謝謝!

最好的答案可以在這里找到,但我認為它只需要一點澄清,因為答案錯誤地暗示了清單的兩種不同的樣式聲明。

如果要從Activity啟動一個對話框並仍然能夠從Preference啟動它,您只需創建一個啟動Dialog的Activity。 然后,可以在Preference XML中或另一個Activity中啟動該Activity。 訣竅在於你如何設計風格。 您希望將“活動”設置為對話框的樣式。 這樣,您的Activity啟動的對話框看起來是正確的。 這種方法的副作用是在Dialog后面的屏幕中間顯示一個浮動的Action Bar。 解決方法是使用沒有ActionBar的Dialog樣式。 我正在使用Holo.Light主題,所以我把它放在我的AndroidManifest中

<activity android:name=".DemoDialogActivity"
            android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar" />

拼圖的另一部分是確保你調用finish(); 當你完成時(這是我在OnClickListener中為我的兩個按鈕做的最后一件事)。 如果不這樣做,對話框將關閉,但活動仍將打開,在黑暗的屏幕中間留下一個小的空白矩形。

這是活動的一個工作示例:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class DemoDialogActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater lf = LayoutInflater.from(this);
        // This adds XML elements as a custom view (optional):
        final View customElementsView = lf.inflate(
                R.layout.activity_credentials, null);
        AlertDialog alert = new AlertDialog.Builder(this)
                // This adds the custom view to the Dialog (optional):
                .setView(customElementsView)
                .setTitle("This is the Title")
                .setMessage("This is the AlertDialog message (optional)")
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // Cancel was clicked; do something
                                // Close Activity
                                finish();
                            }
                        })
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // OK was clicked; do something
                        // Close Activity
                        finish();
                    }
                }).create();

        // Show the dialog
        alert.show();
    }
}

以編程方式啟動它:

Intent launch_dialog = new Intent(getApplicationContext(),
    DemoDialogActivity.class);
startActivity(launch_dialog);

或者作為XML中的首選項:

<Preference
    android:key="demo_dialog"
    android:title="Title of item on Prefs screen"
    android:summary="This will be small text below the title">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.example.package.DemoDialogActivity"
        android:targetPackage="com.example.package" />
</Preference>

我已經有一段時間了。 我發現有一種解決方法完全不優雅。 我可以通過在Activity中進行更改來使用xml布局來構建DialogFragment:

class HCDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_host_credentials, null);
        builder.setView(view);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        })
        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        builder.setTitle(R.string.dialog_title);
        return builder.create();
    }
}

DialogFragment可以從這樣的按鈕啟動(case語句來自作為原始問題的一部分粘貼的代碼):

    case R.id.testing:
        FragmentManager fm = getFragmentManager();
        HCDialog hack_dialog = new HCDialog();
        hack_dialog.show(fm, null);
        return true;

這有效。 但對我來說,這似乎相當愚蠢,我想我已經走了很長一段路。 特別是因為現在我將編碼來處理兩個版本的持久偏好值,這兩個版本似乎是完全相同的對話框。

有沒有更好的辦法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM