Android, How to Remove "NullPointerException" coming in onClick method
Here i wrote some code, and i want to call a method of other class from onClick() and getting NPE in "new Start().moreApplication(feedsId);" line..
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
feeds.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.e("MoreChoices","onclick---------");
final int feedsId= feeds.getId();
Log.e("MoreChoices","onclick----After initialization of id--------");
new Start().moreApplication(feedsId);
Log.e("MoreChoices","onclick----------------After calling--------");
}
});
Start.java is my an other Activity class, and i have one in Start.java, and i want to call that method from other class with using this code开发者_Go百科..
Please tell why i am getting NPE in "new Start().moreApplication(feedsId);" line...
Thanks in Advance please guide me....
Please, take some time reading anddev book, a very good place to start android development.
Read pages 50+ and you will understand that your not doing the right thing to create activities and pass data from one to another.
You should
- startActivity by using either an intent or the class of this activity
- pass data from your activity to the new one using intent's extra parameters.
Regards, Stéphane
replace this line
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
by
final TextView feeds=(TextView)this.findViewById(R.id.more_feeds);
I hope you got solution .
I think u need to get the View ID which u have clicked the enhanced code look like
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
feeds.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.e("MoreChoices","onclick---------");
final int feedsId= arg0.getId();
Log.e("MoreChoices","onclick----After initialization of id--------");
new Start().moreApplication(feedsId);
Log.e("MoreChoices","onclick----------------After calling--------");
}
});
精彩评论