gpt4 book ai didi

android - Webview 在启动时崩溃 android 应用程序

转载 作者:行者123 更新时间:2023-11-30 03:25:20 25 4
gpt4 key购买 nike

我在 Android 应用程序中使用 webview,我试图让它在保存设置后第二次启动该应用程序时自动启动。设置断点后,我能够确定应用程序在 WebActivity 中的“mWebView.setWebChromeClient(new WebChromeClient()”行崩溃。我得到错误源未在调试器中找到。

网络 Activity :

public class WebActivity extends Activity 
{

WebView mWebView;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Activity mActivity = this;

// Adds Progress bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_web);


// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

mWebView = (WebView) findViewById( R.id.webview );

mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);

mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
findViewById(R.id.webview).setVisibility(View.GONE);
mActivity.setTitle("Loading...");
mActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

// Return the app name after finish loading
if(progress == 100)
{
mActivity.setTitle(R.string.webtitle);
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
}
});
Bundle b = getIntent().getExtras();
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient()
{
boolean toast_f = true;
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Context context = getApplicationContext();
CharSequence text = "Loading...(this may take up to 60 seconds)";

if (toast_f)
{
toast_f = false;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER|Gravity.CENTER,0,0);
toast.show();
}

Bundle b = getIntent().getExtras();
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}
});
}

主要 Activity :

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;

SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String GetStatus = sharedPref.getString("DataSaved", "");

if (GetStatus.equals("true"))
{
intent = new Intent(this, WebActivity.class);
}
else
{
intent = new Intent(this, SetupActivity.class);
}

startActivity(intent);
finish();
}

}

这是我在运行应用程序时遇到的 logcat 错误:

08-19 12:25:54.407: W/dalvikvm(15158): threadid=1: thread exiting with uncaught      exception (group=0x4202d438)
08-19 12:25:54.407: E/AndroidRuntime(15158): FATAL EXCEPTION: main
08-19 12:25:54.407: E/AndroidRuntime(15158): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webpage.WebActivity}: java.lang.NullPointerException
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.access$600(ActivityThread.java:138)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.os.Looper.loop(Looper.java:137)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.main(ActivityThread.java:4911)
08-19 12:25:54.407: E/AndroidRuntime(15158): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:25:54.407: E/AndroidRuntime(15158): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-19 12:25:54.407: E/AndroidRuntime(15158): at dalvik.system.NativeStart.main(Native Method)
08-19 12:25:54.407: E/AndroidRuntime(15158): Caused by: java.lang.NullPointerException
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.webpage.WebActivity.onCreate(WebActivity.java:58)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.Activity.performCreate(Activity.java:5240)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
08-19 12:25:54.407: E/AndroidRuntime(15158): ... 11 more

最佳答案

为了更完整的答案:

发生NullPointerException的行是58,正好是

mWebView.loadUrl("http://"(b.getString("iptextfield")) + "/index.html"); 

乍一看,mWebViewb.getString() 或 b 可能是 null

mWebView 之前已经使用过,没有问题,所以它不应该是 null

b.getString() 返回的字符串与其他两个字符串连接在一起,因此不会抛出 NPE。

这样就剩下 b 本身了。

这在 MainActivity 中应该是有意义的,您正在启动 WebActivity 但没有向其 Intent 添加任何额外内容。

使用Intent#putExtra()在开始下一个 Activity 之前向 Intent 添加额外内容。

然后,正如@siik 所建议的,确保您的 Bundle 不为 null 是一个很好的做法:

if (b != null){
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}

您甚至可以更进一步,检查 b 是否确实包含您要查找的 key 。

if (b != null && b.containsKey ("iptextfield")){
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}

关于android - Webview 在启动时崩溃 android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18321772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com