@override 在 android studio in my activity in android studio in android studio in ecommerce app that is from a github demol 中给出错误
@override is giving error in android studio in my activity in android studio in e commerce app that is from a github demol
我有一个购物车片段,我将其转换为 activity,现在它给出了这些错误。
错误:
1.Method 不覆盖其超类的方法(第 86 行)
2.Cannot解析方法'onCreate(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)'(第88行)
CartActivity.java
package com.example.shoppingcart;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.View;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.shoppingcart.R;
import com.example.shoppingcart.adapters.CartListAdapter;
import com.example.shoppingcart.cartholder;
import com.example.shoppingcart.databinding.FragmentCartBinding;
import com.example.shoppingcart.dataholder;
import com.example.shoppingcart.models.CartItem;
import com.example.shoppingcart.productholder;
import com.example.shoppingcart.viewmodels.ShopViewModel;
import com.example.shoppingcart.views.CartFragment;
import com.example.shoppingcart.views.CheckoutActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.zip.Inflater;
import static androidx.core.content.ContentProviderCompat.requireContext;
public class CartActivity extends AppCompatActivity implements CartListAdapter.CartInterface {
private static final String TAG = "CartActivity";
private ImageView productImage;
private TextView productname;
private TextView productprice;
private TextView productcategory;
private Spinner productquantity;
View view;
UUID uuid = UUID.randomUUID();
String randomUUID = uuid.toString().trim();
ShopViewModel shopViewModel;
FragmentCartBinding fragmentCartBinding;
NavController navController;
Button button;
private FirebaseAuth auth;
private FirebaseFirestore firestore;
@Override
protected void onCreate(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState ) {
super.onCreate(inflater,container,savedInstanceState);
fragmentCartBinding = FragmentCartBinding.inflate(inflater, container, false);
setContentView(fragmentCartBinding.getRoot());
auth=FirebaseAuth.getInstance();
firestore=FirebaseFirestore.getInstance();
firestore.collection("Get to cart").document(randomUUID).collection("User").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (DocumentSnapshot doc : task.getResult().getDocuments()){
CartItem myCartModel=doc.toObject(CartItem.class);
}
}
}
});
navController = Navigation.findNavController(view);
final CartListAdapter cartListAdapter = new CartListAdapter(this);
fragmentCartBinding.cartRecyclerView.setAdapter(cartListAdapter);
shopViewModel = new ViewModelProvider(this).get(ShopViewModel.class);
shopViewModel.getCart().observe(this, new Observer<List<CartItem>>() {
@Override
public void onChanged(List<CartItem> cartItems) {
cartListAdapter.submitList(cartItems);
fragmentCartBinding.placeOrderButton.setEnabled(cartItems.size() > 0);
}
});
shopViewModel.getTotalPrice().observe(this, new Observer<Double>() {
@Override
public void onChanged(Double aDouble) {
fragmentCartBinding.orderTotalTextView.setText("Total: PKR " + aDouble.toString());
}
});
button = (Button) findViewById(R.id.placeOrderButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CartActivity.this, CheckoutActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finishActivity();
}
});
}
private void finishActivity() {
if (CartActivity.this != null) {
CartActivity.this.finish();
}
}
public void deleteItem(CartItem cartItem) {
shopViewModel.removeItemFromCart(cartItem);
}
public void changeQuantity(CartItem cartItem, int quantity) {
shopViewModel.changeQuantity(cartItem, quantity);
}
}
activity_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
tools:context=".views.CartFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/cartRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/cart_row"
tools:itemCount="2"
/>
<Space
android:layout_width="match_parent"
android:layout_height="16dp" />
<TextView
android:id="@+id/orderTotalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Total: PKR 26"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6" />
<Button
android:id="@+id/placeOrderButton"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Proceed To Checkout"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />
</LinearLayout>
</ScrollView>
您应该将 onCreate 方法更改为 Activity
的正常变体
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
我有一个购物车片段,我将其转换为 activity,现在它给出了这些错误。
错误:
1.Method 不覆盖其超类的方法(第 86 行)
2.Cannot解析方法'onCreate(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)'(第88行)
CartActivity.java
package com.example.shoppingcart;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.View;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.shoppingcart.R;
import com.example.shoppingcart.adapters.CartListAdapter;
import com.example.shoppingcart.cartholder;
import com.example.shoppingcart.databinding.FragmentCartBinding;
import com.example.shoppingcart.dataholder;
import com.example.shoppingcart.models.CartItem;
import com.example.shoppingcart.productholder;
import com.example.shoppingcart.viewmodels.ShopViewModel;
import com.example.shoppingcart.views.CartFragment;
import com.example.shoppingcart.views.CheckoutActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.zip.Inflater;
import static androidx.core.content.ContentProviderCompat.requireContext;
public class CartActivity extends AppCompatActivity implements CartListAdapter.CartInterface {
private static final String TAG = "CartActivity";
private ImageView productImage;
private TextView productname;
private TextView productprice;
private TextView productcategory;
private Spinner productquantity;
View view;
UUID uuid = UUID.randomUUID();
String randomUUID = uuid.toString().trim();
ShopViewModel shopViewModel;
FragmentCartBinding fragmentCartBinding;
NavController navController;
Button button;
private FirebaseAuth auth;
private FirebaseFirestore firestore;
@Override
protected void onCreate(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState ) {
super.onCreate(inflater,container,savedInstanceState);
fragmentCartBinding = FragmentCartBinding.inflate(inflater, container, false);
setContentView(fragmentCartBinding.getRoot());
auth=FirebaseAuth.getInstance();
firestore=FirebaseFirestore.getInstance();
firestore.collection("Get to cart").document(randomUUID).collection("User").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for (DocumentSnapshot doc : task.getResult().getDocuments()){
CartItem myCartModel=doc.toObject(CartItem.class);
}
}
}
});
navController = Navigation.findNavController(view);
final CartListAdapter cartListAdapter = new CartListAdapter(this);
fragmentCartBinding.cartRecyclerView.setAdapter(cartListAdapter);
shopViewModel = new ViewModelProvider(this).get(ShopViewModel.class);
shopViewModel.getCart().observe(this, new Observer<List<CartItem>>() {
@Override
public void onChanged(List<CartItem> cartItems) {
cartListAdapter.submitList(cartItems);
fragmentCartBinding.placeOrderButton.setEnabled(cartItems.size() > 0);
}
});
shopViewModel.getTotalPrice().observe(this, new Observer<Double>() {
@Override
public void onChanged(Double aDouble) {
fragmentCartBinding.orderTotalTextView.setText("Total: PKR " + aDouble.toString());
}
});
button = (Button) findViewById(R.id.placeOrderButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CartActivity.this, CheckoutActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finishActivity();
}
});
}
private void finishActivity() {
if (CartActivity.this != null) {
CartActivity.this.finish();
}
}
public void deleteItem(CartItem cartItem) {
shopViewModel.removeItemFromCart(cartItem);
}
public void changeQuantity(CartItem cartItem, int quantity) {
shopViewModel.changeQuantity(cartItem, quantity);
}
}
activity_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
tools:context=".views.CartFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/cartRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/cart_row"
tools:itemCount="2"
/>
<Space
android:layout_width="match_parent"
android:layout_height="16dp" />
<TextView
android:id="@+id/orderTotalTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Total: PKR 26"
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6" />
<Button
android:id="@+id/placeOrderButton"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="Proceed To Checkout"
android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />
</LinearLayout>
</ScrollView>
您应该将 onCreate 方法更改为 Activity
的正常变体 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);