我的应用程序中有一个带有不同函数和clases的活动,以及那些保存位图并显示共享对话框的工作。位图保存完美,但是当我点击分享按钮时,FC会给我这个错误
Process: com.javierd.iifym, PID: 3562
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3918)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.javierd.iifym.Utils.RewardsActivity$1.onClick(RewardsActivity.java:59)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是活动:
public class RewardsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void createDialog(final Bitmap bitmap, Context context){
final Dialog dialog = new Dialog(context, android.R.style.Theme_Material_Light_Dialog_Alert);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.goals_dialog);
ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
ImageButton shareButton = (ImageButton) dialog.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String imageFileName = saveBitmapAsImage(bitmap);;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
File media = new File(imageFileName);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share to"));
}
});
imageView.setImageBitmap(bitmap);
dialog.show();
}
private String saveBitmapAsImage(Bitmap imageBitmap){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
File myDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile().toString() + "/IIFYM");
myDir.mkdirs();
final File imageFileName = new File(myDir, "Reward_"+timeStamp+".jpg");
try {
FileOutputStream out = new FileOutputStream(imageFileName);
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return String.valueOf(imageFileName);
}
}
以这种方式从主要活动中调用该代码:
final RewardsActivity rewardsActivity = new RewardsActivity();
rewardsActivity.createDialog(imageBitmap, context);
有人可以帮我吗? 非常感谢!
答案 0 :(得分:0)
您知道Intent吗?
Intent intent = new Intent(AnActivity.this,AnotherActivity.class);
startActivity(intent);
很容易找到一个对话框:像这样:
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
您可以定义布局xml文件。
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
ImageButton shareButton = (ImageButton) dialog.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String imageFileName = saveBitmapAsImage(bitmap);;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
File media = new File(imageFileName);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share to"));
}
});
答案 1 :(得分:0)
请仔细检查我的代码。
public class RewardsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// why not any layout?
}
public void createDialog(final Bitmap bitmap, Context context){
final Dialog dialog = new Dialog(context, android.R.style.Theme_Material_Light_Dialog_Alert);
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.goals_dialog);
dialog.show();
ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView);
ImageButton shareButton = (ImageButton) dialog.findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String imageFileName = saveBitmapAsImage(bitmap);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
File media = new File(imageFileName);
Uri uri = Uri.fromFile(media);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share to"));
}
});
what this view doing here?
imageView.setImageBitmap(bitmap);
}
答案 2 :(得分:0)
好的,最后很简单,我只需将startActivity(Intent.createChooser(share, "Share to"));
更改为context.startActivity(Intent.createChooser(share, "Share to"));