我需要启动一个活动,其中包含一些数据的详细信息,这些数据传递了我在我的Intent上从Firebase数据库获取的参数,但每次我点击适配器上的项目时,我的应用程序都会崩溃。
我有这样的活动:
需要打开这样的东西:
这是我的Adapter.setOnClickListener
代码:
viewHolder.eventCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent eventIntent = new Intent(getContext(), EventActivity.class);
eventIntent.putExtra("event_id", event.getName());
startActivity(eventIntent);
}
});
这是我的新活动代码来获取该参数:
package br.com.ministeriosonhodedeus.sonhodedeus.activity;
import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import br.com.ministeriosonhodedeus.sonhodedeus.R;
import br.com.ministeriosonhodedeus.sonhodedeus.domain.Event;
import br.com.ministeriosonhodedeus.sonhodedeus.fragments.EventFragment;
public class EventActivity extends BaseActivity {
private Event e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
setUpToolbar();
e = getIntent().getParcelableExtra("event_id");
getSupportActionBar().setTitle(e.getName());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView appBarImg = (ImageView) findViewById(R.id.appBarImg);
Picasso.with(getContext()).load(e.geturl_foto()).into(appBarImg);
if (savedInstanceState == null) {
EventFragment frag = new EventFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.EventFragment, frag).commit();
}
}
public void setTitle(String s) {
CollapsingToolbarLayout c = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
c.setTitle(s);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
ActivityCompat.finishAfterTransition(getActivity());
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的错误logcat:
03-21 14:18:12.390 27190-27190/br.com.ministeriosonhodedeus.sonhodedeus E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.ministeriosonhodedeus.sonhodedeus, PID: 27190
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.ministeriosonhodedeus.sonhodedeus/br.com.ministeriosonhodedeus.sonhodedeus.activity.EventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.ministeriosonhodedeus.sonhodedeus.domain.Event.getName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String br.com.ministeriosonhodedeus.sonhodedeus.domain.Event.getName()' on a null object reference
at br.com.ministeriosonhodedeus.sonhodedeus.activity.EventActivity.onCreate(EventActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
知道出了什么问题?谢谢!
答案 0 :(得分:1)
要解决此问题,请更改以下代码行:
e = getIntent().getParcelableExtra("event_id");
与
String event_id = (String) getIntent().getExtras().get("event_id");
和此:
getSupportActionBar().setTitle(e.getName());
与
getSupportActionBar().setTitle(event_id);