Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_post);
- mStorage = FirebaseStorage.getInstance().getReference();
- mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
- mSelectImage = (ImageButton) findViewById(R.id.imageSelect);
- mPostTitle =(EditText)findViewById(R.id.titleField);
- mPostDesc = (EditText)findViewById(R.id.descField);
- mSubmitBtn = (Button)findViewById(R.id.submitBtn);
- grg = (RadioGroup) findViewById(R.id.gendField);
- grb = (RadioButton) findViewById(grg.getCheckedRadioButtonId());
- mProgress = new ProgressDialog(this);
- mSelectImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
- galleryIntent.setType("image/*");
- startActivityForResult(galleryIntent,GALLERY_REQUEST);
- }
- });
- mSubmitBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startposting();
- }
- });
- }
- private void startposting() {
- final String title_val = mPostTitle.getText().toString().trim();
- final String desc_val = mPostDesc.getText().toString().trim();
- final String gend_val = grb.getText().toString().trim();
- if(!TextUtils.isEmpty(title_val)&& !TextUtils.isEmpty(desc_val)&& mImageUri!=null){
- StorageReference filepath = mStorage.child("Blog_images").child(mImageUri.getLastPathSegment());
- filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- @SuppressWarnings("VisibleForTests") Uri downloadUri = taskSnapshot.getDownloadUrl();
- DatabaseReference newPost = mDatabase.push();
- newPost.child("title").setValue(title_val);
- newPost.child("desc").setValue(desc_val);
- newPost.child("image").setValue(downloadUri.toString());
- newPost.child("datetime").setValue(ServerValue.TIMESTAMP);
- newPost.child("gend").setValue(gend_val);
- mProgress.dismiss();
- startActivity(new Intent(PostActivity.this, MainActivity.class));
- }
- }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
- @SuppressWarnings("VisibleForTests") double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
- mProgress.setMessage("Uploading " + ((int) progress) + "%...");
- mProgress.show();
- }
- });
- }
- }
- E/AndroidRuntime: FATAL EXCEPTION: main
- Process: app.testfirebase, PID: 7995
- java.lang.NullPointerException
- at app.testfirebase.PostActivity.startposting(PostActivity.java:87)
- at app.testfirebase.PostActivity.access$000(PostActivity.java:25)
- at app.testfirebase.PostActivity$2.onClick(PostActivity.java:75)
- at android.view.View.performClick(View.java:4438)
- at android.view.View$PerformClick.run(View.java:18422)
- at android.os.Handler.handleCallback(Handler.java:733)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:136)
- at android.app.ActivityThread.main(ActivityThread.java:5001)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:515)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
- at dalvik.system.NativeStart.main(Native Method)
Add Comment
Please, Sign In to add comment