我无法从一个活动到另一个活动获取字符串(包含URL)。 我尝试过使用意图,但是没有用。
我有一个主要活动,用tab设置tabhost。 有3个选项卡,一个带条形码扫描器,一个带搜索栏和webview。
条形码扫描器和搜索字段创建一个包含我希望webview显示的URL的字符串。
有人可以告诉我,我错过了什么或者我怎么能做得更好?
我的主要活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabs() ;
}
private void setTabs()
{
addTab("Home", R.drawable.tab_home, HomeActivity.class);
addTab("Search", R.drawable.tab_search, SearchActivity.class);
addTab("Play", R.drawable.tab_favorite, FavoriteActivity.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
public void switchTab(int tab){
TabHost tabHost1 = getTabHost();
tabHost1.setCurrentTab(tab);
}
webview标签
public class WebviewActivity extends Activity {
WebView webView;
String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favoritepage);
url = "http://www.google.nl";
webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.loadUrl(url);
if (savedInstanceState != null)
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
}
}
和搜索标签
public class SearchActivity extends Activity implements OnClickListener {
Button searchButton;
TextView searchText;
TextView searchResultText;
String zoekopdracht, completeTrailerUrl, searchUrl;
String baseURL = "http://not_needed_here";
String endURL = "End_of_url";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchpage);
searchText=(TextView)findViewById(R.id.searchText);
searchResultText=(TextView)findViewById(R.id.searchResultText);
searchButton=(Button)findViewById(R.id.searchButton);
searchButton.setOnClickListener(this);
}
@Override
public void onClick(View buttonId) {
// TODO Auto-generated method stub
switch (buttonId.getId()){
case R.id.searchButton :
zoekopdracht = searchText.getText().toString();
StringBuilder completeURL = new StringBuilder(baseURL);
completeURL.append(zoekopdracht + endURL);
searchUrl = completeURL.toString();
/**start de thread om de xml zoekopdracht te doen */
new Thread(new Runnable() {
public void run() {
try {
URL website = new URL(searchUrl);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLStuff doingWork = new HandlingXMLStuff();
xr.setContentHandler(doingWork);
InputSource is=new InputSource(website.openStream());
xr.parse(is);
completeTrailerUrl = doingWork.getInformation();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
searchResultText.setText(completeTrailerUrl);
//I want completeTrailerUrl to be loaded in the webview
break;
}
}
}
我已经将接收类改为此类,但我认为这是我出错的地方。
public class FavoriteActivity extends Activity {
WebView webView;
String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favoritepage);
// part submitted by Cool Compiler
Intent t=getIntent();
Bundle k = t.getExtras();
url=k.getString("stringToPassOn");
if (url != null){
url = "http://www.google.nl";
}
webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.loadUrl(url);
if (savedInstanceState != null)
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
}
}
运行时,webview全黑,旋转时崩溃
06-22 13:32:45.380: W/dalvikvm(1630): threadid=1: thread exiting with uncaught exception (group=0x40a51228)
06-22 13:32:45.400: E/AndroidRuntime(1630): FATAL EXCEPTION: main
06-22 13:32:45.400: E/AndroidRuntime(1630): java.lang.RuntimeException: Unable to stop activity {com.georgekroon.player/com.georgekroon.player.TUplayerV2Activity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.georgekroon.player/com.georgekroon.player.FavoriteActivity}: java.lang.NullPointerException
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3505)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3585)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3783)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.access$700(ActivityThread.java:139)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.os.Looper.loop(Looper.java:156)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.main(ActivityThread.java:5005)
06-22 13:32:45.400: E/AndroidRuntime(1630): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:32:45.400: E/AndroidRuntime(1630): at java.lang.reflect.Method.invoke(Method.java:511)
06-22 13:32:45.400: E/AndroidRuntime(1630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-22 13:32:45.400: E/AndroidRuntime(1630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-22 13:32:45.400: E/AndroidRuntime(1630): at dalvik.system.NativeStart.main(Native Method)
06-22 13:32:45.400: E/AndroidRuntime(1630): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.georgekroon.player/com.georgekroon.player.FavoriteActivity}: java.lang.NullPointerException
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1992)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.LocalActivityManager.dispatchStop(LocalActivityManager.java:579)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityGroup.onStop(ActivityGroup.java:82)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1266)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.Activity.performStop(Activity.java:4706)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3500)
06-22 13:32:45.400: E/AndroidRuntime(1630): ... 12 more
06-22 13:32:45.400: E/AndroidRuntime(1630): Caused by: java.lang.NullPointerException
06-22 13:32:45.400: E/AndroidRuntime(1630): at com.georgekroon.player.FavoriteActivity.onCreate(FavoriteActivity.java:25)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.Activity.performCreate(Activity.java:4543)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
06-22 13:32:45.400: E/AndroidRuntime(1630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)
06-22 13:32:45.400: E/AndroidRuntime(1630): ... 19 more
答案 0 :(得分:3)
使用捆绑包。
在您的活动中添加类似
的内容Bundle bundle = new Bundle ();
bundle.putSerializable("stringToPassOn", YOUR_STRING);
Intent xxx;
xxx.putExtras (bundle);
startActivityForResult (xxx, 0);
稍后您将通过以下方式检索信息:
Bundle bundle = this.getIntent().getExtras();
String myText = (String) bundle.getSerializable ("stringToPassOn");
答案 1 :(得分:0)
你需要使用捆绑..然后它必须正常工作。例如;
s=new Intent(Format1.this, Format17.class);
d=new Bundle();
d.putString("userId", userId);
d.putString("surveyId", surveyId);
d.putInt("quesNo", ques_no);
s.putExtras(d);
startActivity(s);
现在在下一课程中,你可以检索这样的值
Intent t=getIntent();
Bundle k=t.getExtras();
surveyId=k.getString("surveyId");
userId=k.getString("userId");
ques_no=k.getInt("quesNo");
试试这个......