0

Hello I have a question about Android Activity.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener (android.view.View$OnClickListener)' on a null object reference

what i want to make is a welcome page .

i have Main_welcome Page called

Activity_main. three page layout is welcome_layout1 welcome_layout2 and welcome_layout3.

i have only one Activity which is the following:

    public class MainActivity extends AppCompatActivity {
    private ViewPager myViewPager;

    private View page1,page2,page3;

    private List<View> pageList;
    Button sign;
    private MypageAdapter mypageAdapter;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Button sign = (Button)this.findViewById(R.id.button_gotosign);
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.sign);
            }
        });
    }
    private void initView (){
        myViewPager = (ViewPager) findViewById(R.id.welcome_viewpager);
        LayoutInflater inflater = getLayoutInflater();
        page1 = inflater.inflate(R.layout.welcome_layout1, null);
        page2 = inflater.inflate(R.layout.welcome_layout2, null);
        page3 = inflater.inflate(R.layout.welcome_layout3, null);
        pageList = new ArrayList();
        pageList.add(page1);
        pageList.add(page2);
        pageList.add(page3);
        mypageAdapter = new MypageAdapter(pageList);
        myViewPager.setAdapter(mypageAdapter);


    }


}

i have a button called sign up at welcome_layout3

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/b3"
    >


    <Button
        android:id="@+id/button_gotosign"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="sign"
        android:textSize="@dimen/activity_vertical_margin"
        android:textColor="#318C83"
        android:background="#90000000"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="34dp">
    </Button>


</RelativeLayout>

what i want to do is , when the user open this APP, he will Roll 3 pictures to the finally one , and there is a button he can click to sign.

how to make the button works?

Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
Junwen Xie
  • 17
  • 4

1 Answers1

0

Your current content view is activity_main and you are accessing Button sign from welcome_layout3. This is your issue. Try to study how to use viewpager from this link https://developer.android.com/training/animation/screen-slide.html and implement.

Febi M Felix
  • 2,611
  • 1
  • 8
  • 13