1

I am working with android activities , and trying to launch an activity when a button is clicked on the previous activity, the problem start in intent object, because the first parameter i.e; context parameter of intent constructor doesn't work with "this, MainActivity.this, getApplicationContext(),getBaseContext" i tried all of the parameters in the first parameter of the intent constructor object. Below is my code.

package com.example.nadeemahmad.guitest;


import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {
    Button ma_sub_btn,prof_sub_btn;
    RelativeLayout ma_rel_lay2,ma_rel_lay3,ma_dots_ly;

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

        ma_sub_btn = (Button)findViewById(R.id.ma_sub_btn);
        ma_rel_lay2 = (RelativeLayout)findViewById(R.id.ma_rel_lay2);
        ma_rel_lay3 = (RelativeLayout)findViewById(R.id.ma_rel_lay3);
        ma_dots_ly = (RelativeLayout)findViewById(R.id.ma_dots_ly);

        ma_sub_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ma_rel_lay2.setVisibility(View.GONE);
                ma_rel_lay3.setVisibility(View.VISIBLE);
                ma_dots_ly.setVisibility(View.GONE);
            }
        });

        prof_sub_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(this, profile.class);
                startActivity(i);
            }
        });


    }




}

Image

Kode Gylaxy
  • 71
  • 1
  • 8

3 Answers3

2

You are getting NullPointerException for Button prof_sub_btn. The problem is you have not initialized Button prof_sub_btn before setting OnClickListener to it..

Try this:

prof_sub_btn = (Button) findViewById(R.id.prof_sub_btn);
prof_sub_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this, profile.class);
            startActivity(i);
        }
    });
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

you have written this inside onClick... try writting MainActivity.this and it should work.. more on using this keyword click here

Intent i = new Intent(MainActivity.this, profile.class);

in your case after watching stacktrace ans should be to bind prof_sub_btn button with xml.

Nilesh Deokar
  • 2,975
  • 30
  • 53
0

Add this line after set content view :

prof_sub_btn = (Button)findViewById(R.id.prof_sub_btn);
Valentin Garcia
  • 143
  • 1
  • 2
  • 10