user1012016
user1012016

Reputation: 79

Logcat error onclicklistener android

I get an error like this in logcat when i try to run my script. I compared it and it is the onClickListener. Any suggestions to fix this problem? Still a beginner.

Upvotes: 0

Views: 184

Answers (4)

CrimsonWorks
CrimsonWorks

Reputation: 11

Instead of using Thread, you can try Handler:

Handler handler = new Handler();

handler.post(new Runnable(){
    public void run(){
        //TODO
    }
});

By the way, posting more exception stack traces would be more helpful.

Upvotes: 0

devunwired
devunwired

Reputation: 63303

The problem exists here:

View splashscreen = (View) findViewById(R.layout.splash);
splashscreen.setOnClickListener(this);

You are getting an exception because splashscreen is null, and calling setOnClickListener() on a null pointer is not allowed. The reason that pointer is null is because you need to obtain a reference to the view from your XML using a proper ID. Your splash.xml file located in res/layout is being loaded as the content view for the Activity, but you should have a proper R.id value associated with that particular view.

In splash.xml, the View you declare for this purpose should have an android:id="@+id/something" attribute in it's XML declaration (I picked "something" out of the air, this identifier can be whatever you want). You would then call:

//Hint: You don't have to cast the result if the pointer is a plain vanilla View
View splashscreen = findViewById(R.id.something);
splashscreen.setOnClickListener(this);

Then you will get a valid reference to the view and your set method will not fail.

HTH

Upvotes: 2

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23606

Yes, Devunwired is right. You must have to give the Resource id to the Perticular view. Instead of that you are going to give the reference of the Layout file name as "splash.xml".

Also try to make the Id name different from the layout file name. Its not and error but sometimes happend that make possible to arrise problem to understand and to give the resource id to different reference.

Thanks.

Upvotes: 0

ductran
ductran

Reputation: 10203

As Devunwired said, I think maybe you wrong about layout and id.

See http://developer.android.com/reference/android/view/View.html

Upvotes: 0

Related Questions