Reputation: 469
Scenario
I am working on a application where, I have a main activity with a button on it. I click that and it loads a canvas in a dialog which I can draw. Upon clicking "ok" on the dialog, I want to return my drawing as base 64 to my main activity.
Problem
The problem with my application is, I cant successfully pass back my base64 string to my main activity.
I have added my Dialog code and the most important part of the main activity. On setDialogResult
When I attempting to pass back the string I am getting a null exception, not sure why.
Would appreciate help in resolving this or assistance in getting my results to my main activity. Dialog code
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);
}
Main Activty (important part)
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);
}
});
}
}
Added the do magic method
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;
}
Log
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)
Upvotes: 1
Views: 85
Reputation: 2252
You should use a callback to get the string from a Fragment
.
This is the recommended method proposed by Android at Communicating with the Activity
For your example, in your DialogFragment
, add an interface and register it.
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");
}
}
Now implement this interface in your 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
}
}
Now in your DialogFragment
, when a user clicks the OK button, send that value back to the Activity
via your callback.
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();
}
});
Upvotes: 1