在 Android Studio 中,我尝试创建一个类似于 Instagram feed 布局的主页,其中显示帖子。但是,我遇到了空指针异常。 ”“
main activity class
package com.example.petapp;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Post> postList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
postList.add(new Post("User1", "Description1", R.drawable.a, "Date1", "Location1", R.drawable.a));
postList.add(new Post("User1", "Description1", R.drawable.a, "Date1", "Location1", R.drawable.a));
// Assuming you have a List<Post> named postList containing your posts
RecyclerView recyclerView = findViewById(R.id.recyclerView);
PostAdapter adapter = new PostAdapter(postList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(this::onNavigationItemSelected);
}
private void initializePostList() {
// Initialize the postList with some dummy data (replace it with your actual data)
// Add more dummy posts as needed
}
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_home) {
// Handle Home button click
Toast.makeText(MainActivity.this, "Home Button Clicked", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.action_add) {
// Handle Explore button click
Toast.makeText(MainActivity.this, "Explore Button Clicked", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.action_profile) {
// Handle Profile button click
Toast.makeText(MainActivity.this, "Profile Button Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
适配器类
package com.example.petapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private List<Post> postList;
public PostAdapter(List<Post> postList) {
this.postList = postList;
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_item, parent, false);
return new PostViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
Post post = postList.get(position);
holder.bind(post);
}
@Override
public int getItemCount() {
return postList.size();
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
private TextView usernameTextView;
private TextView descriptionTextView;
private ImageView imageView;
private TextView dateTextView;
private TextView locationTextView;
private ImageView imgProfileImageView;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
usernameTextView = itemView.findViewById(R.id.usernameMain);
descriptionTextView = itemView.findViewById(R.id.description);
imageView = itemView.findViewById(R.id.imageView2);
dateTextView = itemView.findViewById(R.id.date);
locationTextView = itemView.findViewById(R.id.location);
imgProfileImageView = itemView.findViewById(R.id.profilePic);
}
public void bind(Post post) {
usernameTextView.setText(post.getUsername());
descriptionTextView.setText(post.getDescription());
imageView.setImageResource(post.getImageResource());
dateTextView.setText(post.getDate());
locationTextView.setText(post.getLocation());
imgProfileImageView.setImageResource(post.getImgProfile());
}
}
}
课后
package com.example.petapp;
public class Post {
private String username;
private String description;
private int imageResource;
private String date;
private String location;
private int imgProfile;
public Post(String username, String description, int imageResource, String date, String location, int imgProfile) {
this.username = username;
this.description = description;
this.imageResource = imageResource;
this.date = date;
this.location = location;
this.imgProfile = imgProfile;
}
public String getUsername() {
return username;
}
public String getDescription() {
return description;
}
public int getImageResource() {
return imageResource;
}
public String getDate() {
return date;
}
public String getLocation() {
return location;
}
public int getImgProfile() {
return imgProfile;
}
}
post_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/location"
android:layout_width="186dp"
android:layout_height="28dp"
android:text="alain"
android:textColor="#919191"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/usernameMain"
app:layout_constraintTop_toBottomOf="@+id/usernameMain" />
<TextView
android:id="@+id/usernameMain"
android:layout_width="194dp"
android:layout_height="26dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:text="username"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/cardView"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="8dp"
app:cardCornerRadius="1000dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/profilePic"
android:layout_width="70dp"
android:layout_height="70dp"
android:adjustViewBounds="true"
android:background="@drawable/circle_background"
android:scaleType="centerCrop"
android:src="@drawable/a"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="406dp"
android:layout_height="298dp"
android:scaleType="centerCrop"
android:src="@drawable/a"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/description"
android:layout_width="326dp"
android:layout_height="0dp"
android:layout_marginStart="7dp"
android:text="TextView"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@id/usernameMain"
android:layout_width="72dp"
android:layout_height="28dp"
android:layout_marginStart="5dp"
android:text="username"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/date"
android:layout_width="186dp"
android:layout_height="28dp"
android:text="12/12/2024"
android:textColor="#919191"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/usernameMain"
app:layout_constraintTop_toBottomOf="@+id/usernameMain" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>`
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/sum"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme.PopupOverlay">
<!-- Centered Title -->
<Button
android:id="@+id/textView10"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/line1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/line1"
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:background="@color/white"
android:fadingEdge="horizontal|vertical"
android:fadingEdgeLength="5dp"
android:requiresFadingEdge="horizontal|vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView11"
app:layout_constraintStart_toEndOf="@+id/textView10"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/textView11"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/line2"
app:layout_constraintStart_toEndOf="@+id/textView10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.642" />
<View
android:id="@+id/line2"
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:background="#5A5A5A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView12"
app:layout_constraintStart_toEndOf="@+id/textView11"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/textView12"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="TextView"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView11"
app:layout_constraintTop_toTopOf="parent" />
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="405dp"
android:layout_height="593dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" >
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.recyclerview.widget.RecyclerView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="409dp"
android:layout_height="75dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="409dp"
android:layout_height="71dp"
android:background="@android:color/black"
app:itemIconTint="@android:color/white"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我面临的错误 “2024-03-26 20:01:18.458 8348-8348 AndroidRuntime com.example.petapp D 关闭虚拟机 2024-03-26 20:01:18.458 8348-8348 AndroidRuntime com.example.petapp E 致命异常:main 进程:com.example.petapp,PID:8348 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.petapp/com.example.petapp.MainActivity}:java.lang.NullPointerException:尝试调用接口方法 'boolean java.util.List.add(java. lang.Object)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 引起原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“boolean java.util.List.add(java.lang.Object)” 在 com.example.petapp.MainActivity.onCreate(MainActivity.java:24) 在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.java:7984) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)"
我向 GPT 提供了代码,但它只是产生了无意义的响应。
“我向 GPT 提供了代码,但它只是产生了无意义的响应。”
在实际初始化之前,请执行
postList.add
postList
你可以这样做
private List<Post> postList = new ArrayList<Post>;