从对话框传递字符串时出现问题

时间:2017-03-09 19:20:27

标签: android dialog

方案

我正在开发一个应用程序,我有一个主要活动,上面有一个按钮。 我点击它,它在我可以绘制的对话框中加载画布。点击" ok"在对话框中,我想将我的绘图作为基础64返回到我的主要活动。

问题

我的应用程序的问题是,我无法成功将我的base64字符串传回我的主要活动。

我添加了Dialog代码和主要活动中最重要的部分。在setDialogResult当我尝试传回字符串时,我得到一个空例外,不知道为什么。

非常感谢帮助您解决此问题或协助我将结果发布到我的主要活动中。 对话框代码

        public class DFragment extends DialogFragment {
            private CanvasView customCanvas;
            OnMyDialogResult mDialogResult;  


            Button clearButton;
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.newsignature_layout, container,
                        false);
                getDialog().setTitle("DialogFragment");

                Button clearCanvas = (Button)rootView.findViewById(R.id.newbutton1);
                customCanvas = (CanvasView)rootView.findViewById(R.id.signature_canvas);
                clearCanvas.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        customCanvas.clearCanvas();
                    }
                });


                Button okSignature = (Button)rootView.findViewById(R.id.newbutton3);
                okSignature.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if( mDialogResult != null ){
                           //on onClick of the okSignature button, I will run the "doMagic"
// function in custom canvas that gets the base54 and returns it.

    mDialogResult.finish(String.valueOf(customCanvas.doMagic()));
                        }
                        DFragment.this.dismiss(); 
                    }
                });
                return rootView;
            }

            public void setDialogResult(OnMyDialogResult dialogResult){
                mDialogResult = dialogResult;
            }

            public interface OnMyDialogResult{
                void finish(String result);
            }

主要活动(重要部分)

public class MainActivity extends AppCompatActivity {

    final Context context = this;
    Button dfragbutton;
    Button alertdfragbutton;
    FragmentManager fm = getSupportFragmentManager();
    DFragment dFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        dfragbutton = (Button) findViewById(R.id.dfragbutton);
        alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton);


        dfragbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                DFragment dFragment = new DFragment();

                dFragment.show(fm, "Dialog Fragment");
            }
        });


        });



        dFragment.setDialogResult(new DFragment.OnMyDialogResult(){
            public void finish(String result){

              Log.d("test",result);
            }
        });

    }
}

添加了do magic方法

public String doMagic() {
    Log.d("here","im here");
    View paintview = findViewById(R.id.signature_canvas);
    Log.d("test", "width: " + paintview.getWidth() + " height:" + paintview.getHeight());
    Bitmap bitmap  = Bitmap.createBitmap(paintview.getWidth(), paintview.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    paintview.draw(canvas);
    //encodeTobase64(bitmap);

    Bitmap immagex=bitmap;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    boolean result = immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.NO_WRAP);

    Log.d("test", ""+ result);
    Log.d("test", "baos: " + baos.size());
    Log.d("test", "b: " + b.length);
    Log.d("test", "imageEncoded: " + imageEncoded.length());
    Log.e("test", imageEncoded);

    return imageEncoded;
}

日志

FATAL EXCEPTION: main
                  Process: com.mirage.signaturepad, PID: 5809
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mirage.signaturepad/technologies.mirage.signaturepad.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mirage.signaturepad.DFragment.setDialogResult(com.mirage.signaturepad.DFragment$OnMyDialogResult)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      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:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void technologies.mirage.signaturepad.DFragment.setDialogResult(com.mirage.signaturepad.DFragment$OnMyDialogResult)' on a null object reference
                      at technologies.mirage.signaturepad.MainActivity.onCreate(MainActivity.java:101)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      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:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

1 个答案:

答案 0 :(得分:1)

您应该使用回调来从Fragment获取字符串。 这是Android在Communicating with the Activity

提出的推荐方法

对于您的示例,请在DialogFragment中添加一个界面并进行注册。

public static interface OnMyDialogResult{
    public abstract void onFinish(String value);
}

private OnMyDialogResult mListener;

// make sure the Activity implemented it
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity); 
    try {
        this.mListener = (OnMyDialogResult)activity;
    }
    catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnMyDialogResult");
    }
}

现在在Activity

中实现此界面
public class MainActivity extends Activity implements DFragment.OnMyDialogResult{
    //...

    public void onfinish(String value) {
        // After the dialog fragment completes, it calls this callback.
        // use the string here
    }
}

现在在DialogFragment,当用户点击“确定”按钮时,通过回调将该值发回Activity

okSignature.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if( mDialogResult != null ){
                       //on onClick of the okSignature button, I will run the "doMagic"
// function in custom canvas that gets the base54 and returns it.

this.mListener.onFinish(String.valueOf(customCanvas.doMagic()));
                    }
                    DFragment.this.dismiss(); 
                }
            });