因此,我创建了一个RecyclerView
,然后在其上增加了一个布局individual_user.xml
。
在AllUsersActivity
中,我正在将数据连接到Firebase。
我正在尝试在单个用户的容器上设置一个OnClickListener
和一个individualUserContainerConstraintLayout
来设置一个Intent。但是,我不断收到NullPointerException
错误。为什么会这样?
我要与Activity
一起使用的Intent(UserProfileActivity)
只有一个TextView
。
在旁注:我是这个论坛的新手,所以我希望我的问题足够详细。我确实搜索了论坛上已经存在的类似问题,但是这些问题都没有帮助。
AllUsersActivity:
public class AllUsersActivity extends AppCompatActivity {
private Toolbar mAllUsersToolBar;
private RecyclerView mAllUsersRecyclerView;
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseUsersAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
//To place ToolBar on the page
mAllUsersToolBar = findViewById(R.id.allUsersToolBar);
setSupportActionBar(mAllUsersToolBar);
getSupportActionBar().setTitle("All Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAllUsersRecyclerView = findViewById(R.id.allUsersRecyclerView);
//When user clicks the container of the user on the All Users page, they get sent to that particular user's user profile.
ConstraintLayout individualUserContainerConstraintLayout = (ConstraintLayout) findViewById(R.id.individualUserContainerConstraintLayout);
individualUserContainerConstraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToUserProfile = new Intent(AllUsersActivity.this, UserProfileActivity.class);
startActivity(goToUserProfile);
}
});
//FirebaseAdapter
//Query locates the file you want to access the data from.
Query query = FirebaseDatabase.getInstance().getReference().child("Users");
FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(query, Users.class)
.build();
firebaseUsersAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
options) {
@Override
protected void onBindViewHolder(UsersViewHolder holder, int position, Users model) {
holder.mIndividualUserName.setText(model.getUsername());
holder.mIndividualUserStatus.setText(model.getStatus());
holder.mIndividualUserPicture.setImageDrawable(getDrawable(R.drawable.profile_pic));
//This is use to retrieve the User ID from Firebase Database.
String userId = getRef(position).getKey();
}
@Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.individual_user, parent, false);
return new UsersViewHolder(view);
}
};
mAllUsersRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAllUsersRecyclerView.setAdapter(firebaseUsersAdapter);
firebaseUsersAdapter.startListening();
}
@Override
protected void onResume() {
super.onResume();
if(firebaseUsersAdapter != null){
firebaseUsersAdapter.startListening();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(firebaseUsersAdapter != null){
firebaseUsersAdapter.stopListening();
}
}
}
错误:
09-10 21:16:38.593 2311-2311/? E/adbd: failed to connect to socket 'localabstract:com.example.android.whammychat': Connection refused
09-10 21:16:39.475 1575-6243/? E/AudioFlinger: not enough memory for AudioTrack size=131296
09-10 21:16:39.475 1575-6243/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
09-10 21:16:39.477 2412-7979/? E/IAudioFlinger: createRecord returned error -12
09-10 21:16:39.477 2412-7979/? E/AudioRecord: AudioFlinger could not create record track, status: -12
09-10 21:16:39.478 2412-7979/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
09-10 21:16:39.478 2412-7979/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
09-10 21:16:39.479 2412-7979/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
09-10 21:16:47.742 1663-1851/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
09-10 21:16:47.858 1663-1695/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
09-10 21:16:53.374 8098-8098/com.example.android.whammychat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.whammychat, PID: 8098
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.whammychat/com.example.android.whammychat.AllUsersActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.android.whammychat.AllUsersActivity.onCreate(AllUsersActivity.java:48)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
09-10 21:16:53.490 1799-1886/? E/InputDispatcher: channel '1450866 PopupWindow:2083893 (server)' ~ Channel is unrecoverably broken and will be disposed!
09-10 21:16:53.492 1799-1886/? E/InputDispatcher: channel '118f06 com.example.android.whammychat/com.example.android.whammychat.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
09-10 21:16:54.429 1575-6243/? E/AudioFlinger: not enough memory for AudioTrack size=131296
09-10 21:16:54.429 1575-6243/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
09-10 21:16:54.440 2412-8127/? E/IAudioFlinger: createRecord returned error -12
09-10 21:16:54.440 2412-8127/? E/AudioRecord: AudioFlinger could not create record track, status: -12
09-10 21:16:54.440 2412-8127/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
09-10 21:16:54.441 2412-8127/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
09-10 21:16:54.441 2412-8127/? E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
09-10 21:16:54.480 1799-1819/? E/memtrack: Couldn't load memtrack module
答案 0 :(得分:0)
只有一个问题可能导致此。请检查以下行:
ConstraintLayout individualUserContainerConstraintLayout = (ConstraintLayout) findViewById(R.id.individualUserContainerConstraintLayout);
确保individualUserContainerConstraintLayout
实际上是该组件的ID
而且znát布局isclickable = true
答案 1 :(得分:0)
您的问题可能是individualUserContainerConstraintLayout
是R.layout.individual_user
的一部分,而您正试图在R.layout.activity_all_users
中找到它。
这不是RecyclerView
的工作方式。您需要在RecyclerView的适配器中设置Click侦听器。在这里您可以访问R.layout.individual_user
内部的视图。
一种方法是这样的:
首先,您需要删除这些行,因为它们没有帮助:
ConstraintLayout individualUserContainerConstraintLayout = (ConstraintLayout) findViewById(R.id.individualUserContainerConstraintLayout);
individualUserContainerConstraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToUserProfile = new Intent(AllUsersActivity.this, UserProfileActivity.class);
startActivity(goToUserProfile);
}
});
现在,您可以在onBindViewHolder(UsersViewHolder, int, Users)
的方法末尾添加以下行:
holder.itemView.setOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToUserProfile = new Intent(AllUsersActivity.this, UserProfileActivity.class);
startActivity(goToUserProfile);
}
});
尝试一下,看看是否可行。如果它不起作用,则可能是因为您在此方法中对“活动”没有正确的可见性。如果是这样,您应该使用一个界面,无论如何我还是会推荐这种解决方案。
让我知道您是否也需要帮助。