user70568
user70568

Reputation:

What am I forgetting about OnClickListener?

I'm knocking together what amounts to an RSS reader in Android, and I'm having a problem with onClickListener.

The following code run fine outside of the onClickListener, but as soon as I move it inside the class it

        topNewsbtn = (Button) findViewById(R.id.newsTop);

    topNewsbtn.setOnClickListener(new OnClickListener(){


        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent itemintent = new Intent(that,RSSReader.class);
            Bundle b = new Bundle();
            b.putString("FeedURL", "http://english.aljazeera.net/Services/Rss/?PostingId=2007731105943979989");
            itemintent.putExtra("android.intent.extra.INTENT", b);
            startActivity(itemintent);
        }


    }); 

N.B. that is defined in the Activity as a Context and within onCreate assigned as that = this

On execution the app crashed on start-up with the following stack trace (nothing in LogCat)

Thread [<1> main] (Suspended (exception java.lang.RuntimeException))    
    <VM does not provide monitor information>   
    android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityRecord, android.content.Intent) line: 2663  
    android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityRecord, android.content.Intent) line: 2679   
    android.app.ActivityThread.access$2300(android.app.ActivityThread, android.app.ActivityThread$ActivityRecord, android.content.Intent) line: 125 
    android.app.ActivityThread$H.handleMessage(android.os.Message) line: 2033   
    android.app.ActivityThread$H(android.os.Handler).dispatchMessage(android.os.Message) line: 99   
    android.os.Looper.loop() line: 123  
    android.app.ActivityThread.main(java.lang.String[]) line: 4627  
    java.lang.reflect.Method.invokeNative(java.lang.Object, java.lang.Object[], java.lang.Class, java.lang.Class[], java.lang.Class, int, boolean) line: not available [native method]  
    java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object...) line: 521    
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() line: 868  
    com.android.internal.os.ZygoteInit.main(java.lang.String[]) line: 626   
    dalvik.system.NativeStart.main(java.lang.String[]) line: not available [native method]  

I have another onClickListener that also crashes with the same sort of stack trace and that looks like

        watchLivebtn =(Button) findViewById(R.id.Live);
    watchLivebtn.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {
            WebView browser = (WebView)findViewById(R.id.watchnowElement);

            String HTML = "Just some HTML I want to display";
            browser.loadData(HTML, "text/html", "UTF-8");

            setContentView(R.layout.watchnow);

        }
    });

So clearly I'm doing something that wrong with the setOnClickListener, but what?

Thanks.

update

I resisted puting the whole class in because it's so long, as the two people who've tryed to help me (Thanks) have pointed out stuff I've don't I'll post the whole thing to help you all help me.

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.SlidingDrawer;


public class AJE extends Activity {

Button news_slideHandleButton;
SlidingDrawer news_slidingDrawer;


Button topNewsbtn;
Button watchLivebtn;

Context that;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    that = this;
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    news_slideHandleButton = (Button) findViewById(R.id.news_slideHandleButton);
    news_slidingDrawer = (SlidingDrawer) findViewById(R.id.news_SlidingDrawer);


    topNewsbtn = (Button) findViewById(R.id.newsTop);

    topNewsbtn.setOnClickListener(new OnClickListener(){


        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent itemintent = new Intent(that,RSSReader.class);
            Bundle b = new Bundle();
            b.putString("FeedURL", "http://english.aljazeera.net/Services/Rss/?PostingId=2007731105943979989");
            itemintent.putExtra("android.intent.extra.INTENT", b);
            startActivity(itemintent);
        }


    }); 

    watchLivebtn =(Button) findViewById(R.id.Live);
    watchLivebtn.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0) {
            WebView browser = (WebView)findViewById(R.id.watchnowElement);

            String HTML = "<html><body><script src='http://admin.brightcove.com/js/BrightcoveExperiences.js' type='text/javascript'></script>" +
                          "<script src='/custom/brightcove/aje_bc.js' type='text/javascript'></script>"+
                          "<div style='text-align: left; padding-bottom: 20px;'>" +
                          "<div id='BCplayerArea' style='width: 680px; height: 420px;'><object class='BrightcoveExperience' id='myExperience747084146001' data='http://c.brightcove.com/services/viewer/federated_f9?&amp;width=680&amp;height=440&amp;flashID=myExperience747084146001&amp;bgcolor=%23FFFFFF&amp;playerID=751182905001&amp;playerKey=AQ~~%2CAAAAmtVJIFk~%2CTVGOQ5ZTwJYW4Aj2VxnKEXntSbmcf9ZQ&amp;isVid=true&amp;isUI=true&amp;dynamicStreaming=true&amp;%40videoPlayer=747084146001&amp;autoStart=' type='application/x-shockwave-flash' width='680' height='440'><param value='always' name='allowScriptAccess'><param value='true' name='allowFullScreen'><param value='false' name='seamlessTabbing'><param value='true' name='swliveconnect'><param value='window' name='wmode'><param value='high' name='quality'><param value='#FFFFFF' name='bgcolor'></object></div>"+
                          "<script type='text/javascript'>// <![CDATA["+
                          "RenderScVideo('747084146001','751182905001',680,440,'BCplayerArea');"+ 
                          "brightcove.createExperiences();    // ]]></script></body></html>';";
            browser.loadData(HTML, "text/html", "UTF-8");

            setContentView(R.layout.watchnow);

        }
    });

    setContentView(R.layout.start);
    }
}

Upvotes: 2

Views: 3311

Answers (3)

Octavian Helm
Octavian Helm

Reputation: 39605

Inside of an inner class this will get you the reference to the class you are currently in and not to the Activity.

Initialize a Context object inside of your Activity's onCreate() method to use it inside of the inner class.

There is another possibility which is not encouraged. I'm only showing you the possibilities and repeat myself that it is not encouraged to use this method as you might leak the entire Activity depending on the code you are passing that context to.

You could get the Activity like this MyActivity.this. It would work but don't do it that way as long as you are not 100% sure know that you are not going to leak it.


UPDATE

Your problem is something else in fact. You are setting the layout after accessing it which won't work. So you have to set the layout using setContentView() preferably directly after the up-call to super.

The main problem is you are trying to access views that simply are not there yet which makes the application crash of course.

Upvotes: 2

Tosa
Tosa

Reputation: 658

As basicly said by Octavian Damiean.

public void onCreate(Bundle savedInstanceState) {    
    that = this;
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    /*
     if R.id.news_slideHandleButton and R.id.news_SlidingDrawer are on this view 
     it has to be set before try to access elements on it
    */
    setContentView(R.layout.start);

    news_slideHandleButton = (Button) findViewById(R.id.news_slideHandleButton);
    news_slidingDrawer = (SlidingDrawer) findViewById(R.id.news_SlidingDrawer);

    // the listeners go here
}

Upvotes: 0

Macarse
Macarse

Reputation: 93163

Are you sure you are importing:

import android.view.View.OnClickListener;

What's the meaning of that in:

Intent itemintent = new Intent(that,RSSReader.class);

Upvotes: 0

Related Questions