我正在尝试创建一个RSS应用程序,我将在其中打开一个带有WebView相关文章的活动。我使用Intent putExtra可能不起作用......任何人都有解决方案的建议吗?
谢谢!
ShowNews.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ShowNews extends Activity{
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.show_news);
String oneNews = null;
String nTitle = null;
String nDesc = null;
String nDate = null;
String nLink = null;
Intent i = getIntent();
if(i != null)
{
Bundle b = i.getBundleExtra("BUNDLE.VISA.SEN.INTENT");
if(b == null)
{
oneNews = "Fel bundle?";
} else {
nTitle = b.getString("title");
nDesc = b.getString("description");
nDate = b.getString("pubdate");
nLink = b.getString("link");
}
} else {
oneNews = "Information Not Found.";
}
TextView tTitle = (TextView)findViewById(R.id.title);
TextView tDesc = (TextView)findViewById(R.id.desc);
TextView tDate = (TextView)findViewById(R.id.pubdate);
TextView tLink = (TextView)findViewById(R.id.link);
tTitle.setText(nTitle);
tDesc.setText(nDesc);
tDate.setText(nDate);
tLink.setText(nLink);
final String iLink = nLink;
tLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShowNews.this, Browser.class);
i.putExtra("link", String.valueOf(iLink));
startActivity(i);
}
});
}
}
Browser.java
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
@SuppressLint("SetJavaScriptEnabled")
public class Browser extends Activity {
private WebView mWebView;
//String i = getIntent().getStringExtra("link");
Bundle extras = getIntent().getExtras();
String URLEN = extras.getString("link");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(URLEN);
mWebView.setWebViewClient(new WebClient());
}
public class WebClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
错误
01-12 22:55:53.328: E/AndroidRuntime(12532): FATAL EXCEPTION: main
01-12 22:55:53.328: E/AndroidRuntime(12532): Process: com.example.com.raj.rss, PID: 12532
01-12 22:55:53.328: E/AndroidRuntime(12532): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.com.raj.rss/com.example.com.raj.rss.Browser}: java.lang.NullPointerException
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread.access$800(ActivityThread.java:138)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.os.Handler.dispatchMessage(Handler.java:102)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.os.Looper.loop(Looper.java:136)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread.main(ActivityThread.java:5050)
01-12 22:55:53.328: E/AndroidRuntime(12532): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 22:55:53.328: E/AndroidRuntime(12532): at java.lang.reflect.Method.invoke(Method.java:515)
01-12 22:55:53.328: E/AndroidRuntime(12532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
01-12 22:55:53.328: E/AndroidRuntime(12532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
01-12 22:55:53.328: E/AndroidRuntime(12532): at dalvik.system.NativeStart.main(Native Method)
01-12 22:55:53.328: E/AndroidRuntime(12532): Caused by: java.lang.NullPointerException
01-12 22:55:53.328: E/AndroidRuntime(12532): at com.example.com.raj.rss.Browser.<init>(Browser.java:20)
01-12 22:55:53.328: E/AndroidRuntime(12532): at java.lang.Class.newInstanceImpl(Native Method)
01-12 22:55:53.328: E/AndroidRuntime(12532): at java.lang.Class.newInstance(Class.java:1208)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-12 22:55:53.328: E/AndroidRuntime(12532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
01-12 22:55:53.328: E/AndroidRuntime(12532): ... 11 more
答案 0 :(得分:2)
您无法在类级别使用意图,您必须在onCreate
中使用它(以便在使用之前运行onCreate
)。
public class Browser extends Activity {
private WebView mWebView;
Bundle extras;
String URLEN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
extras = getIntent().getExtras();
URLEN = extras.getString("link");
mWebView = (WebView)findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(URLEN);
mWebView.setWebViewClient(new WebClient());
}
// .. the rest of the class
}
答案 1 :(得分:0)
将其移至onCreate()
Bundle extras = getIntent().getExtras();
String URLEN = extras.getString("link");
至少onCreate()
之前,您无法访问该意图。