我的Android应用在

时间:2018-01-10 22:44:32

标签: java android xml android-layout

我的Android应用程序在打开时停止,但Android工作室显示没有错误。有人可以帮忙吗?我试图将我的网站转换为Android应用程序。我对此有点新意。

以下是我的MainActivity.java代码

的显示
package com.example.deedat.webtry2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
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 {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView = (WebView)findViewById(R.id.webView);
        myWebView.loadUrl("http://www.motivationalscholar.com");
        myWebView.setWebViewClient(new WebViewClient());


    }

    @Override
    public void onBackPressed() {
        if(myWebView.canGoBack()){
            myWebView.goBack();
        } 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.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);
    }
}

这是我的activity_main.xml代码

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.deedat.webtry2.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

我的Logcat显示了这个:

01-10 19:26:15.552 17316-17316 /?我/ zygote:没有迟到-Xcheck:jni(已经开启) 01-10 19:26:15.562 17316-17316 /? W / zygote:使用默认值的X86的意外CPU变体:x86 01-10 19:26:15.793 17316-17316 / com.example.deedat.webtry2 I / InstantRun:启动即时运行服务器:是主进程 01-10 19:26:15.929 17316-17316 / com.example.deedat.webtry2 D / AndroidRuntime:关闭VM 01-10 19:26:15.932 17316-17316 / com.example.deedat.webtry2 E / AndroidRuntime:FATAL EXCEPTION:main                                                                             处理:com.example.deedat.webtry2,PID:17316                                                                             java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.deedat.webtry2 / com.example.deedat.webtry2.MainActivity}:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0 :错误膨胀类uses-permission                                                                                 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)                                                                                 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                                 在android.app.ActivityThread.-wrap11(未知来源:0)                                                                                 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                 在android.os.Looper.loop(Looper.java:164)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                              引起:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类uses-permission                                                                              引起:android.view.InflateException:二进制XML文件行#0:错误膨胀类uses-permission                                                                              引起:java.lang.ClassNotFoundException:没找到类&#34; android.view.uses-permission&#34;在路径上:DexPathList [[zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / base.apk&#34;,zip文件&#34; / data / app /com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g==/split_lib_dependencies_apk.apk" ;, zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g== /split_lib_slice_0_apk.apk" ;, zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / split_lib_slice_1_apk.apk&#34;,zip文件&#34; / data /app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g==/split_lib_slice_2_apk.apk" ;, zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / split_lib_slice_3_apk.apk&#34;,zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / split_lib_slice_4_apk.apk&#34;,zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g==/split_lib_slice_5_apk.apk" ;, zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M -SNtV1g == / split_lib_slice_6_apk.apk&#34;,zip文件&#34; / dat a / app / com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / split_lib_slice_7_apk.apk&#34;,zip file&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M- SNtV1g == / split_lib_slice_8_apk.apk&#34;,zip文件&#34; /data/app/com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / split_lib_slice_9_apk.apk&#34;],nativeLibraryDirectories = [/ data / app / com.example.deedat.webtry2-qFM_P6edDm6em-M-SNtV1g == / lib / x86,/ system / lib,/ system / vendor / lib]]                                                                                 在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)                                                                                 at java.lang.ClassLoader.loadClass(ClassLoader.java:379)                                                                                 在java.lang.ClassLoader.loadClass(ClassLoader.java:312)                                                                                 在android.view.LayoutInflater.createView(LayoutInflater.java:606)                                                                                 在android.view.LayoutInflater.onCreateView(LayoutInflater.java:703)                                                                                 在com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)                                                                                 在android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)                                                                                 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)                                                                                 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)                                                                                 在android.view.LayoutInflater.rInflate(LayoutInflater.java:863)                                                                                 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)                                                                                 在android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)                                                                                 在android.view.LayoutInflater.rInflate(LayoutInflater.java:859)                                                                                 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)                                                                                 在android.view.LayoutInflater.inflate(LayoutInflater.java:515)                                                                                 在android.view.LayoutInflater.inflate(LayoutInflater.java:423)                                                                                 在android.view.LayoutInflater.inflate(LayoutInflater.java:374)                                                                                 在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)                                                                                 在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 01-10 19:26:15.933 17316-17316 / com.example.deedat.webtry2 E / AndroidRuntime:at com.example.deedat.webtry2.MainActivity.onCreate(MainActivity.java:22)                                                                                 在android.app.Activity.performCreate(Activity.java:6975)                                                                                 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)                                                                                 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)                                                                                 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                                 在android.app.ActivityThread.-wrap11(未知来源:0)                                                                                 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:105)                                                                                 在android.os.Looper.loop(Looper.java:164)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我的Android应用程序在打开时停止,但Android工作室显示没有错误。有人可以帮忙吗?我试图将我的网站转换为Android应用程序。我对此有点新意。

1 个答案:

答案 0 :(得分:0)

您的onCreate()方法就是这样开始的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebSettings webSettings = myWebView.getSettings();
    ...
}

myWebView.getSettings()几乎肯定会抛出NullPointerException,因为您从未初始化myWebView。在setContentView()之后添加此行:

myWebView = (WebView) findViewById(R.id.webView);