错误:无法从TContinuationResult,TResult是类型变量的静态上下文中引用非静态方法continueWithTask(Continuation>):

**这是continueWithTask错误所在的代码**

private void StoreProductInformation()
{

    loadingBar.setTitle("Add New Product");
    loadingBar.setMessage("Dear Admin,Please wait....., while we are adding the new Product");
    loadingBar.setCanceledOnTouchOutside(false);
    loadingBar.show();

    Calendar calendar=Calendar.getInstance();

    SimpleDateFormat CurrentDate=new SimpleDateFormat("MM DD,YYYY");
    SaveCurrentDate=CurrentDate.format(calendar.getTime());

    SimpleDateFormat CurrentTime=new SimpleDateFormat("HH:mm:ss a");
    SaveCurrentTime=CurrentTime.format(calendar.getTime());

    ProductRsndomKey=SaveCurrentDate + SaveCurrentTime;

    final StorageReference filePath = ProdductImageRef.child(ImageUri.getLastPathSegment() + ProductRsndomKey + ".jpg");

    final UploadTask uploadTask=filePath.putFile(ImageUri);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e)
        {
            String message=e.toString();
            Toast.makeText(AdminAddNewProductActivity.this,"Error: "+message,Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
        {
            Toast.makeText(AdminAddNewProductActivity.this,"Image uploaded successfully... ",Toast.LENGTH_SHORT).show();

            Task<Uri> uriTask = UploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                {
                    if(!task.isSuccessful())
                    {
                        throw task.getException();
                    }
                    downloadImageUrl=filePath.getDownloadUrl().toString();
                    return filePath.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if(task.isSuccessful())
                    {
                        downloadImageUrl=task.getResult().toString();

                        Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
                        SaveProductInfoToDatabase();
                    }
                }
            });
        }
    });

}


我的整个代码

package com.example.ecommerce;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

public class AdminAddNewProductActivity extends AppCompatActivity {

private String CategoryName , Description,Price,Pname,SaveCurrentDate,SaveCurrentTime;
private ImageView InputProductImage;
private Button AddNewProductButton;
private EditText InputProductName,InputProductDescription,InputProductPrice;
private static final int GalleryPick=1;
private Uri ImageUri;
private String ProductRsndomKey , downloadImageUrl;
private StorageReference ProdductImageRef;
private DatabaseReference ProductsRef;
private ProgressDialog loadingBar;

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

    CategoryName=getIntent().getExtras().get("category").toString();
    ProdductImageRef = FirebaseStorage.getInstance().getReference().child("Product Images");
    ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");

    AddNewProductButton=findViewById(R.id.add_new_product);
    InputProductImage=findViewById(R.id.select_products_image);
    InputProductName=findViewById(R.id.product_name);
    InputProductDescription=findViewById(R.id.product_description);
    InputProductPrice=findViewById(R.id.product_price);
    loadingBar=new ProgressDialog(this);

    InputProductImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            OpenGallary();
        }
    });
    AddNewProductButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
           ValidateProductData();
        }
    });
}

private void ValidateProductData()
{
    Description=InputProductDescription.getText().toString();
    Price=InputProductPrice.getText().toString();
    Pname=InputProductName.getText().toString();
    if(ImageUri == null)
    {
        Toast.makeText(this,"Product Image is required...",Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(Description))
    {
        Toast.makeText(this,"Please write product description...",Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(Price))
    {
        Toast.makeText(this,"Please write product Price...",Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(Pname))
    {
        Toast.makeText(this,"Please write product Name...",Toast.LENGTH_SHORT).show();
    }
    else
    {
        StoreProductInformation();
    }
}

private void StoreProductInformation()
{

    loadingBar.setTitle("Add New Product");
    loadingBar.setMessage("Dear Admin,Please wait....., while we are adding the new Product");
    loadingBar.setCanceledOnTouchOutside(false);
    loadingBar.show();

    Calendar calendar=Calendar.getInstance();

    SimpleDateFormat CurrentDate=new SimpleDateFormat("MM DD,YYYY");
    SaveCurrentDate=CurrentDate.format(calendar.getTime());

    SimpleDateFormat CurrentTime=new SimpleDateFormat("HH:mm:ss a");
    SaveCurrentTime=CurrentTime.format(calendar.getTime());

    ProductRsndomKey=SaveCurrentDate + SaveCurrentTime;

    final StorageReference filePath = ProdductImageRef.child(ImageUri.getLastPathSegment() + ProductRsndomKey + ".jpg");

    final UploadTask uploadTask=filePath.putFile(ImageUri);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e)
        {
            String message=e.toString();
            Toast.makeText(AdminAddNewProductActivity.this,"Error: "+message,Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
        {
            Toast.makeText(AdminAddNewProductActivity.this,"Image uploaded successfully... ",Toast.LENGTH_SHORT).show();

            Task<Uri> uriTask = UploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                {
                    if(!task.isSuccessful())
                    {
                        throw task.getException();
                    }
                    downloadImageUrl=filePath.getDownloadUrl().toString();
                    return filePath.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if(task.isSuccessful())
                    {
                        downloadImageUrl=task.getResult().toString();

                        Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
                        SaveProductInfoToDatabase();
                    }
                }
            });
        }
    });

}

private void SaveProductInfoToDatabase()
{
    HashMap<String,Object> productMap=new HashMap<>();
    productMap.put("pid",ProductRsndomKey);
    productMap.put("date",SaveCurrentDate);
    productMap.put("time",SaveCurrentTime);
    productMap.put("discription",Description);
    productMap.put("image",downloadImageUrl);
    productMap.put("category",CategoryName);
    productMap.put("price",Price);
    productMap.put("pname",Pname);

    ProductsRef.child(ProductRsndomKey).updateChildren(productMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task)
        {
            if ((task.isSuccessful()))
            {
                Intent intent=new Intent(AdminAddNewProductActivity.this,AdminCategoryActivity.class);
                startActivity(intent);
                loadingBar.dismiss();
                Toast.makeText(AdminAddNewProductActivity.this,"Product is added Successfully...",Toast.LENGTH_SHORT).show();
            }
            else
            {
                loadingBar.dismiss();
                String messg=task.getException().toString();
                Toast.makeText(AdminAddNewProductActivity.this,"Error :"+messg,Toast.LENGTH_SHORT).show();

            }
        }
    });
}

private void OpenGallary()
{
    Intent galleryIntent=new Intent();
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(galleryIntent,GalleryPick);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GalleryPick && resultCode == RESULT_OK && data != null)
    {
        ImageUri=data.getData();
        InputProductImage.setImageURI(ImageUri);

    }
}


}

最佳答案

continueWithTask是实例方法,不是静态方法,因此您需要使用实例变量uploadTask来调用continueWithTask

final UploadTask uploadTask=filePath.putFile(ImageUri);

Task<Uri> uriTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                {
                    if(!task.isSuccessful())
                    {
                        throw task.getException();
                    }
                    downloadImageUrl=filePath.getDownloadUrl().toString();
                    return filePath.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if(task.isSuccessful())
                    {
                        downloadImageUrl=task.getResult().toString();

                        Toast.makeText(AdminAddNewProductActivity.this,"got the Product image , save to Database Successfully...",Toast.LENGTH_SHORT).show();
                        SaveProductInfoToDatabase();
                    }
                }
            });

10-08 11:34