簡體   English   中英

嘗試在AlertDialog按鈕上顯示Toast時,應用程序崩潰(在片段中)

[英]App crashing while trying to display a Toast on AlertDialog button click (in a Fragment)

我正在嘗試使DialogFragment包含警報對話框。 它包含一個接口,該接口包含一個應在按下對話框的“取消/確定”按鈕時調用的方法。

現在的問題是,我想在用戶單擊“確定” /“取消”時顯示Toast.makeText,但是我不知道應該傳遞給Toast的上下文是什么,以免崩潰。 當用戶單擊對話框按鈕時,我的應用程序崩潰了,我猜是上下文導致了異常:

片段類:

public class AlertDFragment extends DialogFragment {

dialogListener ds;
Context con;

public interface dialogListener {
    public void onOK(Context c);
    public void onCancel(Context c);
}

@Override
public Dialog onCreateDialog(Bundle savedInstances) {
    Builder b = new AlertDialog.Builder(getActivity());

    b.setIcon(R.drawable.abc_ic_search);
    b.setTitle("DialogFragment instance");
    b.setMessage("Choose an option");
    b.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            ds.onOK(con);
        }
    });

    b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            ds.onCancel(con);
        }
    });

    return b.create();
}

public AlertDFragment() {

}

protected void OnAttach(Activity activity) {
    super.onAttach(activity);
    con = activity;
    try {
        ds = (dialogListener) activity;
    } catch(ClassCastException e) {
        throw new ClassCastException(activity.toString()+" must implement the dialogListener interface");
    }
}

}

主要活動(片段的父對象):

public class MainActivity extends FragmentActivity implements AlertDFragment.dialogListener {

Button dialog2;
FragmentManager fm = getSupportFragmentManager();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dialog2 = (Button) findViewById(R.id.btn2);
    dialog2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            AlertDFragment adf = new AlertDFragment();
            adf.show(fm, "MY TAG");
        }
    });

}

//interface methods
public void onOK(Context c) {
    Toast.makeText(MainActivity.this, "OK pressed", Toast.LENGTH_SHORT).show();     
}

public void onCancel(Context c) {
    Toast.makeText(MainActivity.this, "Cancel pressed", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

和LogCat:

11-04 10:23:30.959: E/AndroidRuntime(2014): FATAL EXCEPTION: main
11-04 10:23:30.959: E/AndroidRuntime(2014): Process: com.apex.dialogtest, PID: 2014
11-04 10:23:30.959: E/AndroidRuntime(2014): java.lang.NullPointerException
11-04 10:23:30.959: E/AndroidRuntime(2014):     at com.apex.dialogtest.AlertDFragment$1.onClick(AlertDFragment.java:33)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at android.os.Looper.loop(Looper.java:136)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at java.lang.reflect.Method.invoke(Method.java:515)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-04 10:23:30.959: E/AndroidRuntime(2014):     at dalvik.system.NativeStart.main(Native Method)

提前致謝!

ds未初始化,因為protected void OnAttach(Activity activity)需要小寫的o。 確保始終使用@Override注釋,否則會出現錯誤。

您在代碼中具有以下內容

dialogListener ds;

但是您永遠不會實例化它。 然后調用ds.onOK(con); 參考ds上的方法。 這將導致空指針異常。

暫無
暫無

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

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