Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我忘记了什么?_Android - Fatal编程技术网

Android 我忘记了什么?

Android 我忘记了什么?,android,Android,我正在组装Android中的RSS阅读器,而onClickListener有问题 下面的代码在onClickListener外部运行良好,但只要我将其移动到类中,它就会立即运行 topNewsbtn = (Button) findViewById(R.id.newsTop); topNewsbtn.setOnClickListener(new OnClickListener(){ public void onClick(View arg0) {

我正在组装Android中的RSS阅读器,而onClickListener有问题

下面的代码在onClickListener外部运行良好,但只要我将其移动到类中,它就会立即运行

        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);
        }


    }); 
注意:在活动中定义为上下文,在onCreate中指定为that=this

执行时,应用程序在启动时崩溃,堆栈跟踪如下(LogCat中无任何内容)

很明显我对setOnClickListener做了一些错误的事情,但是什么呢

谢谢

更新

我拒绝让全班同学参与进来,因为时间太长了,因为两个试图帮助我的人(谢谢)指出了一些我不知道的东西。我将发布整个事件,以帮助大家帮助我

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);
    }
}
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.Window;
导入android.webkit.WebView;
导入android.widget.Button;
导入android.widget.SlidingDrawer;
公共类AJE扩展活动{
按钮新闻\u滑动手柄按钮;
滑动抽屉新闻\u滑动抽屉;
按钮topNewsbtn;
按钮手表;
上下文;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
那=这个;
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
news_slideHandleButton=(按钮)findViewById(R.id.news_slideHandleButton);
news\u slidingDrawer=(slidingDrawer)findViewById(R.id.news\u slidingDrawer);
topNewsbtn=(按钮)findViewById(R.id.newsTop);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图arg0){
//TODO自动生成的方法存根

Intent itemintent=新的Intent(即RSSReader.class); Bundle b=新Bundle(); b、 putString(“FeedURL”)http://english.aljazeera.net/Services/Rss/?PostingId=2007731105943979989"); itemintent.putExtra(“android.intent.extra.intent”,b); startActivity(itemintent); } }); watchLivebtn=(按钮)findviewbyd(R.id.Live); setOnClickListener(新的OnClickListener(){ 公共void onClick(视图arg0){ WebView浏览器=(WebView)findViewById(R.id.watchnowElement); 字符串HTML=“”+ ""+ "" + ""+ "// ';"; 加载数据(HTML、“文本/HTML”、“UTF-8”); setContentView(R.layout.watchnow); } }); setContentView(R.layout.start); } }
您确定要导入:

import android.view.View.OnClickListener;
的含义是什么

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

在内部类的内部
将为您提供对当前所在类的引用,而不是对活动的引用

活动
onCreate()方法中初始化
上下文
对象,以便在内部类中使用它

还有一种可能性是不被鼓励的。我只是向您展示了这些可能性,并重申我不鼓励使用这种方法,因为您可能会泄漏整个活动,这取决于您将上下文传递给的代码

你可以得到这样的活动
MyActivity.this
。它会工作,但不要这样做,只要你不是100%肯定知道你不会泄漏它


更新

事实上,你的问题是别的。访问布局后,您正在设置布局,但布局不起作用。因此,您必须使用
setContentView()
设置布局,最好是在向上调用super之后直接设置布局


主要问题是,您试图访问的视图根本不存在,这当然会导致应用程序崩溃。

正如屋大维·达米扬(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
}

我只是想添加(我不认为这是重新发明,但…)第一个按钮在一个滑动抽屉中,所以在启动时隐藏。这是从哪里来的:“Thread[main](Suspended(exception java.lang.RuntimeException))”?调试器?@Bert,是的Eclipse调试器,他们对LogCat不感兴趣,只是debug选项卡中的threadsuspended消息……我想补充一点。通常,在使用
super
关键字调用基类之前执行一些操作不是一个好主意。一般来说,你应该先打电话,然后做任何你必须做的事情。@Scott-Hmm,这就是我的想法。我认为调试器在打印异常之前挂起了线程。如果是这样,让调试器继续线程,线程将异常和完整堆栈跟踪打印回logcat。如果我是对的,堆栈跟踪将是一个很大的帮助。“Intent itemintent=newintent(that,RSSReader.class);”启动一个新活动(RSSReader)并将一些数据传递给它(提要的URL),这是一个上下文,在onCreate中设置为this。@Scott Herbert:将
this
设置为
this
有什么意义。这是非常不标准的,正如在这个答案中所看到的,可能会非常混乱。@Falmari因为我需要将活动的上下文传递给新的意图,因为它在子类(onClickListener)中被调用,如果我使用
this
是传递内部类的上下文,而不是活动。请参阅Octavian Damiean更新前答案,以获得更好的措辞。@Scott Herbert:那么,与其这样称呼它,不如将其命名为
ctx
,或其他描述性变量。而且,您甚至不需要该变量,因为您可以通过执行
AJE来访问活动的
this
指针。this
@falmari,AJE。这很危险,可能会泄漏。。。此外,我不会称ctx更具描述性,如果我想这样做,我会称它为copyOfMainActivityContext,但我没有…是的,移动

Intent itemintent = new Intent(that,RSSReader.class);
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
}