0

I am trying to call intent I set putExtra in first Activity as follows. In first activity

public void onClick(View v) {
    int position = getAdapterPosition();
    final Intent intent;
    intent = new Intent(c, WhenCardClicked.class);
    intent.putExtra("title",titles[position]);
    intent.putExtra("desc",details[position]);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    c.startActivity(intent);
}

In WhenCardClicked.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_when_card_clicked);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TextView textView = (TextView) findViewById(R.id.title);
    TextView textView1 = (TextView) findViewById(R.id.desc);
    String title ="";
    String desc = "";
    title = getIntent().getExtras().getString("title");
    desc = getIntent().getExtras().getString("desc");
    Log.d("Debugtext", title+" "+desc);
    if(title != null)textView.setText(title);
}

In content_card_demo.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:id="@+id/title"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/desc"
android:layout_below="@+id/title"/>

I am calling intent in first activity. But I am getting

Null pointer exceptioava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at quickbook.com.recyclerlayout.WhenCardClicked.onCreate(WhenCardClicked.java:28)

sushildlh
  • 8,986
  • 4
  • 33
  • 77

1 Answers1

1

For other people encoutering this:

Okay, the nullpointer is on one of your TextViews. Maybe because you setContentView to "activity_when_card_clicked" but the layouts name is "content_card_demo.xml".

You just referenced the wrong layout in setContentView().

G. Kalender
  • 491
  • 4
  • 13