gpt4 book ai didi

java - 无法启动 Activity ComponentInfo (LOGCAT)

转载 作者:行者123 更新时间:2023-12-02 03:47:41 24 4
gpt4 key购买 nike

我是一名新的 Android 程序员,最近,我在 logcat 中收到此错误:

03-22 15:33:49.395 13639-13639/com.abaco.awser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abaco.awser, PID: 13639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abaco.awser/com.abaco.awser.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.abaco.awser.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
at android.app.ActivityThread.access$800(ActivityThread.java:166) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5584) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
at dalvik.system.NativeStart.main(Native Method) 

这是我的 MainActivity.java:

package com.abaco.awser;

import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

@Override
public void onBackPressed() {
}

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

WebView myWebView = (WebView) findViewById(R.id.activity_main_webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://primopizza.com.br/eloja");

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

setContentView(R.layout.activity_main);
}
}

这是我的 Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abaco.abawser.MainActivity">

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

</RelativeLayout>

你能帮我解决这个错误吗?我已经尝试了一切。

最佳答案

您收到 NPE 因为您的 myWebViewnull放置这个

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

之后

setContentView(R.layout.activity_main);

因为只有设置内容 View 后才能真正检索 View

关于java - 无法启动 Activity ComponentInfo (LOGCAT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162940/

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