我正在敲响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);
}
});
N.B。在活动中定义为上下文,在onCreate中定义为= this
执行时,应用程序在启动时崩溃,出现以下堆栈跟踪(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]
我还有另一个onClickListener,它也崩溃了同样的堆栈跟踪,看起来像
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);
}
});
很明显我正在做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?&width=680&height=440&flashID=myExperience747084146001&bgcolor=%23FFFFFF&playerID=751182905001&playerKey=AQ~~%2CAAAAmtVJIFk~%2CTVGOQ5ZTwJYW4Aj2VxnKEXntSbmcf9ZQ&isVid=true&isUI=true&dynamicStreaming=true&%40videoPlayer=747084146001&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);
}
}
答案 0 :(得分:3)
在内部类this
内部,您将获得对当前所在类的引用,而不是活动。
在Context
的{{1}}方法中初始化Activity
对象,以便在内部类中使用它。
还有另一种可能性不被鼓励。我只向您展示了可能性,并重复自己不鼓励使用此方法,因为您可能会泄漏整个Activity,具体取决于您将该上下文传递给的代码。
您可以像onCreate()
一样获得此活动。只要你不是100%肯定知道你不会泄漏它,它会起作用但不要这样做。
<强>更新强>
事实上你的问题是别的。您在访问后设置布局,这将无法正常工作。因此,您必须使用MyActivity.this
设置布局,最好是在向上调用后直接进行布局。
主要问题是您正在尝试访问那些尚未导致应用程序崩溃的视图。
答案 1 :(得分:0)
您确定要导入:
import android.view.View.OnClickListener;
that
的含义是什么:
Intent itemintent = new Intent(that,RSSReader.class);
答案 2 :(得分:0)
正如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
}