Jomia
Jomia

Reputation: 3404

android: problem when including a layout

I have included one header view to every views in my application. And also can implement button click etc to this header with the help of answer :

Include in android using java

But my requirement is on button click, it should start another activity. when I click the button, it stopped suddently. But other activitites like Toast, button.setText() etc is working fine on button click.

Home.class

 TextView txtMarket = (TextView) findViewById(R.id.txtMarket);
 TextView txtNews = (TextView) findViewById(R.id.txtNews);
 ImageButton imgLogout=(ImageButton)findViewById(R.id.imgLogout);
 ImageButton imgDashBoard=(ImageButton)findViewById(R.id.imgDashBoard);
 Header objHeader=new Header(imgLogout,txtMarket,txtNews,imgDashBoard,getBaseContext());
 objHeader.init();

Header.class

 //in Header class
 public Header(ImageButton btnLogin,TextView txtMarket,TextView txtNews,ImageButton imgDashBoard,Context context) 
 {     
          this.btnLogin = btnLogin; 
     this.txtMarket = txtMarket;     
     this.txtNews = txtNews; 
     this.imgDashBoard = imgDashBoard; 
     this.context=context;
 }
 public void init() 
 {  
      btnLogin.setOnClickListener(new View.OnClickListener()
 {              
           public void onClick(View view)
      {
            Intent loginIntent=new Intent();                               Toast.makeText(context, "u clicked the button", 1).show();
       loginIntent.setClass(view.getContext(),login.class);
       startActivity(loginIntent);
           }
});
}

Please help me friends....

Edited

When I am defining like :

Intent loginIntent=new Intent(Home.this,login.class);

then it shows an error :

"No enclosing instance of the type loginHome is accessible in scope"

And if I am giving like :

When I am defining like :

Intent loginIntent=new Intent(Header.this,login.class);

then it caught NullPointerException

Upvotes: 1

Views: 466

Answers (2)

Dalmas
Dalmas

Reputation: 26547

Try to use the context variable from your constructor :

loginIntent.setClass(Header.this.context,login.class);

Upvotes: 0

Thrawn80
Thrawn80

Reputation: 351

Did you edit the manifest.xml with inserting your new activity? Like:

<activity android:name=".NewActivityName" />

Upvotes: 1

Related Questions