【发布时间】:2020-01-26 17:12:16
【问题描述】:
我在RecyclerView 中放置了一个按钮以将其移至下一个活动,但是当我单击该按钮时,它什么也没做,包括不将其移至下一个活动。
我尝试将按钮放在RecyclerView 之外,但是当RecyclerView 出现时,按钮已经消失。我尝试修改所有关于RecyclerView 的代码,但没有成功。
这是展示它的Activity
package com.example.rsolveapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class RsolverMatch extends AppCompatActivity {
private DatabaseReference database;
private RecyclerView rvView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Description> daftarBarang;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rsolvermatch);
rvView = (RecyclerView) findViewById(R.id.rv_main);
rvView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
rvView.setLayoutManager(layoutManager);
database = FirebaseDatabase.getInstance().getReference();
database.child("description").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
daftarBarang = new ArrayList<>();
for (DataSnapshot noteDataSnapshot :
dataSnapshot.getChildren()) {
Description description =
noteDataSnapshot.getValue(Description.class);
description.setKey(noteDataSnapshot.getKey());
daftarBarang.add(description);
}
adapter = new AdapterDescRecyclerView(daftarBarang,
RsolverMatch.this);
rvView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println(databaseError.getDetails() + " " +
databaseError.getMessage());
}
});
}
public static Intent getActIntent(Activity activity){
return new Intent(activity, RsolverMatch.class);
}
}
这是按钮的第一个 .xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
android:background="@drawable/background1">
<android.support.v7.widget.CardView
android:id="@+id/cv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="10dp"
android:layout_gravity="center_horizontal"
card_view:cardBackgroundColor="@color/background_material_light"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="2.5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_margin="8dp"
android:id="@+id/descText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description : "
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_margin="8dp"
android:id="@+id/tv_namabarang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_margin="8dp"
android:id="@+id/dayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Working Day :"
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_margin="8dp"
android:id="@+id/tv_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_margin="8dp"
android:id="@+id/daysHour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Working Hour : "
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_marginTop="8dp"
android:id="@+id/tv_shour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_marginTop="8dp"
android:id="@+id/tv_smin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_margin="8dp"
android:id="@+id/dayeHour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Working Hour : "
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_marginTop="8dp"
android:id="@+id/tv_ehour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textColor="@color/black"
android:textSize="16sp"/>
<TextView
android:layout_marginTop="8dp"
android:id="@+id/tv_emin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<Button
android:id="@+id/takeJob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Take Job"
android:background="@color/green"
android:layout_marginTop="10dp"/>
</LinearLayout>
这是按钮的第二个 .xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background1">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</LinearLayout>
预期的结果是按钮可以被点击并移动到下一个活动。 这是 logcat 中的错误消息:
E/AndroidRuntime: 致命异常: main 进程:com.example.rsolveapp,PID:13737 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.rsolveapp/com.example.rsolveapp.RsolverMatch}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6692) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 引起:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 com.example.rsolveapp.RsolverMatch.onCreate(RsolverMatch.java:56) 在 android.app.Activity.performCreate(Activity.java:7140) 在 android.app.Activity.performCreate(Activity.java:7131) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6692) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是我的 AdapterRecyclerView 活动
public class AdapterDescRecyclerView extends
RecyclerView.Adapter<AdapterDescRecyclerView.ViewHolder> {
private ArrayList<Description> daftarBarang;
private Context context;
public AdapterDescRecyclerView(ArrayList<Description> barangs, Context ctx){
daftarBarang = barangs;
context = ctx;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle;
TextView tvday;
TextView tvshour;
TextView tvsmin;
TextView tvehour;
TextView tvemin;
CardView cvMain;
ViewHolder(View v) {
super(v);
tvTitle = (TextView) v.findViewById(R.id.tv_namabarang);
tvday = (TextView) v.findViewById(R.id.tv_day);
tvshour = (TextView) v.findViewById(R.id.tv_shour);
tvsmin = (TextView) v.findViewById(R.id.tv_smin);
tvehour = (TextView) v.findViewById(R.id.tv_ehour);
tvemin = (TextView) v.findViewById(R.id.tv_emin);
cvMain = (CardView) v.findViewById(R.id.cv_main);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job,
parent, false);
// mengeset ukuran view, margin, padding, dan parameter layout lainnya
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final String name = daftarBarang.get(position).getDescription();
final String day = daftarBarang.get(position).getDay();
final String shour = daftarBarang.get(position).getsHour();
final String smin = daftarBarang.get(position).getsMin();
final String ehour = daftarBarang.get(position).geteHour();
final String emin = daftarBarang.get(position).geteMin();
holder.tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
holder.tvTitle.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
holder.tvTitle.setText(name);
holder.tvday.setText(day);
holder.tvshour.setText(shour);
holder.tvsmin.setText(smin);
holder.tvehour.setText(ehour);
holder.tvemin.setText(emin);
}
@Override
public int getItemCount() {
return daftarBarang.size();
}
}
这是我的 Rsolvermatch Activity,它已经影响了 onclicklistener 但导致错误
package com.example.rsolveapp;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class RsolverMatch extends AppCompatActivity implements
View.OnClickListener {
private DatabaseReference database;
private RecyclerView rvView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Description> daftarBarang;
Button takeJob;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rsolvermatch);
rvView = (RecyclerView) findViewById(R.id.rv_main);
rvView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
rvView.setLayoutManager(layoutManager);
takeJob.setOnClickListener(this);
takeJob = findViewById(R.id.takeJob);
database = FirebaseDatabase.getInstance().getReference();
database.child("description").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
daftarBarang = new ArrayList<>();
for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren())
{
Description description =
noteDataSnapshot.getValue(Description.class);
description.setKey(noteDataSnapshot.getKey());
daftarBarang.add(description);
}
adapter = new AdapterDescRecyclerView(daftarBarang,
RsolverMatch.this);
rvView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println(databaseError.getDetails() + " " +
databaseError.getMessage());
}
});
}
@Override
public void onClick(View v) {
if (v == takeJob) {
Intent intent = new Intent(RsolverMatch.this, Chatting.class);
startActivity(intent);
}
}
public static Intent getActIntent (Activity activity){
return new Intent(activity, RsolverMatch.class);
}
}
【问题讨论】:
-
显示适配器“AdapterDescRecyclerView”的代码
-
按钮的点击监听器在哪里,还要添加适配器的代码。
-
你有点忘了添加包含
setOnClickListener的相关代码。 -
@Makarand 适配器已添加到我的代码摘要中
-
@EpicPandaForce 已经添加了 onclicklistener 但我收到了错误,所以我再次删除了它
标签: android firebase firebase-realtime-database