-3

I am trying to create a register application that lists attendees (in TextView). On Clicking an attendee, it should take to another view. Getting a NullpointerException during runtime at OnclickListener.

Mail View Layout xml :

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#ffffff"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:textStyle="bold"
    android:clickable="true"
    android:text="text1" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#ffffff"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:textStyle="bold"
    android:clickable="true"
    android:text="text2" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#ffffff"
    android:layout_weight="1"
    android:textStyle="bold"
    android:clickable="true"
    android:gravity="center_horizontal"
    android:text="text3" />

OnClick View xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/img1"/>

Main Activity :

public class MenuActivity extends ListActivity {

TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    text1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.onclickview);
        }
    });
}

}

Ahmed Abidi
  • 1,017
  • 12
  • 24
  • duplicate of : http://stackoverflow.com/questions/41321754/android-null-pointer-exception-on-my-database – Ahmed Abidi Dec 26 '16 at 16:56
  • duplicate of http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Noorul Dec 26 '16 at 17:16

2 Answers2

0

You haven't set any id's to TextView in your layout file.You are also missing the initialization part in your java file. First set id's in your layout file then initialize the required TextViews and then only you can call onClickListener() method

Aamir
  • 256
  • 1
  • 2
  • 13
0
public class MenuActivity extends ListActivity {

TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    ////add your textview id
    text1=(TextView )findViewById(R.id.yourtextviewID);
    text1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.onclickview);
        }
   });
}
kamal verma
  • 456
  • 2
  • 16