使用WebView进行应用崩溃

时间:2016-04-21 19:33:23

标签: webview crash

请帮帮我。我想在我的应用程序中放置一个WebView。如果我做一个新项目它工作正常。但是,当我想把它放在另一个应用程序中它崩溃。

包含Mainactivity / Java文件/布局文件/ logcat

MainActivity

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
private WebView mWebView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webaanhoudingskaart);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.nav_algemeen_toestand) {
       fragmentManager.beginTransaction()
               .replace(R.id.content_frame
               , new Algemeen_Toestand())
               .commit();
    } else if (id == R.id.nav_algemeen_opdracht) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Opdracht())
                .commit();
    } else if (id == R.id.nav_algemeen_uitvoering) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Uitvoering())
                .commit();
    } else if (id == R.id.nav_algemeen_verzoring) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Verzorging())
                .commit();
    } else if (id == R.id.nav_algemeen_tunue) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Tunue())
                .commit();
    } else if (id == R.id.nav_algemeen_bevelvoering) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Bevelvoering())
                .commit();
    } else if (id == R.id.nav_algemeen_scenario) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Algemeen_Scenario())
                .commit();
    } else if (id == R.id.nav_operationeel_wedstrijdinformatie) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Wedstrijdinformatie())
                .commit();
    } else if (id == R.id.nav_operationeel_tijdsschema) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Tijdsschema())
                .commit();
    } else if (id == R.id.nav_operationeel_indeling) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Indeling())
                .commit();
    } else if (id == R.id.nav_operationeel_verbindingsschema) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Verbindingsschema())
                .commit();
    } else if (id == R.id.nav_operationeel_stdaion) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Stadion())
                .commit();
    } else if (id == R.id.nav_operationeel_positie) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Positie())
                .commit();
    } else if (id == R.id.nav_operationeel_aanhoudingskaart) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Aanhoudingskaart())
                .commit();
    } else if (id == R.id.nav_operationeel_stdionverboden) {
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame
                        , new Operationeel_Stadionverboden())
                .commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

爪哇

package remco.politiewillemiiapp;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;





/**
 * Created by Remco on 6-4-2016.
  */
public class Operationeel_Aanhoudingskaart extends Fragment {
View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.operationeel_aanhoudingskaart, container, false);
    return myView;

}

布局          

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webaanhoudingskaart" />
 </RelativeLayout>

logcat的

04-21 21:27:41.151 29717-29717/? E/Zygote: v2
04-21 21:27:41.151 29717-29717/? I/libpersona: KNOX_SDCARD checking this for 10375
04-21 21:27:41.151 29717-29717/? I/libpersona: KNOX_SDCARD not a persona
04-21 21:27:41.151 29717-29717/? I/SELinux: Function: selinux_compare_spd_ram, index[1], SPD-policy is existed. and_ver=SEPF_SM-G920F_5.1.1 ver=38
04-21 21:27:41.151 29717-29717/? W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [2], priority version is VE=SEPF_SECMOBILE_6.0.1_0004
04-21 21:27:41.151 29717-29717/? E/Zygote: accessInfo : 0
04-21 21:27:41.151 29717-29717/? W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=remco.politiewillemiiapp 
04-21 21:27:41.151 29717-29717/? I/art: Late-enabling -Xcheck:jni
04-21 21:27:41.181 29717-29717/? D/TimaKeyStoreProvider: TimaSignature is unavailable
04-21 21:27:41.181 29717-29717/? D/ActivityThread: Added TimaKeyStore provider
04-21 21:27:41.221 29717-29717/remco.politiewillemiiapp W/ResourcesManager: getTopLevelResources: /data/app/remco.politiewillemiiapp-2/base.apk / 1.0 running in remco.politiewillemiiapp rsrc of package remco.politiewillemiiapp
04-21 21:27:41.221 29717-29717/remco.politiewillemiiapp I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
04-21 21:27:41.221 29717-29717/remco.politiewillemiiapp D/ResourcesManager: For user 0 new overlays fetched Null
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp I/InjectionManager: Inside getClassLibPath caller 
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp W/System: ClassLoader referenced unknown path: /data/app/remco.politiewillemiiapp-2/lib/arm64
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp D/InjectionManager: InjectionManager
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp D/InjectionManager: fillFeatureStoreMap remco.politiewillemiiapp
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp I/InjectionManager: Constructor remco.politiewillemiiapp, Feature store :{}
04-21 21:27:41.241 29717-29717/remco.politiewillemiiapp I/InjectionManager: featureStore :{}
04-21 21:27:41.251 29717-29717/remco.politiewillemiiapp W/ResourcesManager: getTopLevelResources: /data/app/remco.politiewillemiiapp-2/base.apk / 1.0 running in remco.politiewillemiiapp rsrc of package remco.politiewillemiiapp
04-21 21:27:41.251 29717-29717/remco.politiewillemiiapp W/ResourcesManager: getTopLevelResources: /data/app/remco.politiewillemiiapp-2/base.apk / 1.0 running in remco.politiewillemiiapp rsrc of package remco.politiewillemiiapp
04-21 21:27:41.321 29717-29717/remco.politiewillemiiapp D/AbsListView: Get MotionRecognitionManager
04-21 21:27:41.321 29717-29717/remco.politiewillemiiapp E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@c948c05
04-21 21:27:41.321 29717-29717/remco.politiewillemiiapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ec7545a
04-21 21:27:41.321 29717-29717/remco.politiewillemiiapp E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@ec7545a
04-21 21:27:41.821 29717-29717/remco.politiewillemiiapp D/skia: ---- fAsset->read(2769) returned 0
04-21 21:27:41.831 29717-29717/remco.politiewillemiiapp D/AndroidRuntime: Shutting down VM
04-21 21:27:41.831 29717-29717/remco.politiewillemiiapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: remco.politiewillemiiapp, PID: 29717
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{remco.politiewillemiiapp/remco.politiewillemiiapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
                                                                          at remco.politiewillemiiapp.MainActivity.onCreate(MainActivity.java:26)
                                                                          at android.app.Activity.performCreate(Activity.java:6876)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:158) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

1 个答案:

答案 0 :(得分:0)

  

您尚未初始化您的网页设置,因此它会显示错误&#34;空对象参考&#34;。请初始化它并重新运行项目。

$this->db->where('nh_bookings.booking_req_by_date_time < DATE_ADD(now(), INTERVAL 2 DAY) AND nh_bookings.booking_req_by_date_time > NOW()');