0

I dont know why this code throws a nullPointerException. I did not written this part of code and im pretty new with this staff. I tried my best but could not able to find an answer.

public class ProgramExamActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    setTitle("Screen #1");
}
public void onClick(View v) {       
    switch(v.getId()) {     
        case R.id.btn2:
                setContentView(R.layout.screen2);
                setTitle("Screen #2");
                break;
    }
}
4
  • 1
    Post Exception log.. with your question.
    – user370305
    Jul 7, 2012 at 12:05
  • Logs can be more helpful for helpers :)
    – AAnkit
    Jul 7, 2012 at 12:07
  • Although code written badly, With things which should be avoid.
    – AAnkit
    Jul 7, 2012 at 12:08
  • propably its stupid question but where i can find log in eclipse?
    – Enoras
    Jul 7, 2012 at 13:00

2 Answers 2

2

First, this

setContentView(R.layout.screen2);
setTitle("Screen #2");

you can't, you shouldn't call this more than once.

And second if you want to use OnClickListener, you need to register it for some widget and in your code there is any widget.

In your case your class need to implement View.OnClickListener if you don' want to work with Listeners as anonymous classes

public class Program... extends Activity implements View.OnClickListener { ... }

Then you need to register it like this:

Button btn = (Button) findViewById(R.id.btn2);
btn.setOnClickListener(this);

Note: if you want to start another Activity with different title and content, you have to use Intents and call startActivity().

More about Intents and there is tutorial Android: How to switch between Activities.

2
  • @Enoras >> Yes well written answer, I would like to make one more point here,as Deceiver suggested not to set contentView twice . You Should actually start a new activity instead of setting new content view. and show your layout in new activity.
    – AAnkit
    Jul 7, 2012 at 12:16
  • thanks for quick answer and link to tutorial. I tried to register, but it says that it have wrong tokens. I going to read all tutorials again, becouse i have feeling you waste your time with me.
    – Enoras
    Jul 7, 2012 at 13:05
0

Add listener of click event as

public class ProgramExamActivity extends Activity implements OnClickListener{

then, register your component with this listener. Suppose you want to add click on any of your Button like btn1. then add code in OnCreate

btn1.setOnClickListener(ProgramExamActivity.this);
2
  • the same answer already given before 8 min, You should not post duplicate answer, if there is nothing new and unique in your answer Please avoid posting it.
    – AAnkit
    Jul 7, 2012 at 12:18
  • Ok I marked and UPVOTED "deceiver"'s answer. It was my mistake that I have not read whole answer. Jul 7, 2012 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.