あるアクティビティから別のアクティビティへの文字列 (URL を含む) の取得に問題があります。インテントを使用してみましたが、うまくいきませんでした。
タブでタブホストを設定する主なアクティビティがあります。3 つのタブがあり、1 つはバーコード スキャナー、もう 1 つは検索フィールドと Web ビューです。
バーコード スキャナーと検索フィールドは、Web ビューに表示する 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);
}
ウェブビュータブ
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