gpt4 book ai didi

java - 不幸的是,项目已在 android studio 中停止

转载 作者:行者123 更新时间:2023-12-01 17:17:08 26 4
gpt4 key购买 nike

该代码的全部内容是在第一个 Activity 中将星座作为输入,单击“确定”后将导航到下一个 Activity ,在下一个 Activity 中将详细信息显示为该特定星座的 Web View 。当我尝试在模拟器或手机中运行代码时,我会收到警告项目正在不断停止并且应用程序关闭。

MainActivity.java

package com.example.project;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void send(View v){
Intent goToSecond = new Intent();
goToSecond.setClass(this, SecondActivity.class);
startActivity(goToSecond);
String sign = ((EditText)(findViewById(R.id.editText_sign))).getText().toString();
goToSecond.putExtra("sign", sign);
startActivity(goToSecond);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="100"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity"
tools:ignore="Autofill"
android:background="@drawable/signwheel2">


<TextView
android:id="@+id/textView"
android:layout_width="56dp"
android:layout_height="wrap_content"
android:layout_marginStart="55dp"
android:background="#EFE9E9"
android:text="@string/zodiac" />

<EditText
android:id="@+id/editText_sign"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="#FDFCFC"
android:ems="10"
android:hint="@string/libra"
android:inputType="textPersonName" />

<Button
android:id="@+id/button2"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:onClick="send"
android:text="@android:string/ok"
android:visibility="visible" />
</LinearLayout>

SecondActivity.java

package com.example.project;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
webView = findViewById(R.id.webview);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent caller = getIntent();
String sign = caller.getStringExtra("sign");
assert sign != null;
switch (sign) {
case "Taurus":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/taurus-zodiac-sign/");
break;
case "Aries":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/aries-zodiac-sign/");
break;
case "Leo":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/leo-zodiac-sign/");
break;
case "Pisces":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/pisces-zodiac-sign/");
break;
case "Scoripo":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/scorpio-zodiac-sign/");
break;
case "Sagittarius":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/sagittarius-zodiac-sign/");
break;
case "Gemini":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/gemini-zodiac-sign/");
break;
case "Virgo":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/virgo-zodiac-sign/");
break;
case "Libra":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/libra-zodiac-sign/");
break;
case "Capricorn":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/capricorn-zodiac-sign/");
break;
case "Aquarius":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/aquarius-zodiac-sign/");
break;
case "Cancer":
webView.loadUrl("https://www.chatelaine.com/living/horoscopes/cancer-zodiac-sign/");
break;
default:
String message1 = "Please check the spelling";
Toast.makeText(getApplicationContext(), message1, Toast.LENGTH_LONG).show();
break;
}

}
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".SecondActivity">

<WebView
android:id="@+id/webview"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

日志猫:

04-22 14:59:17.888 3355-3355/? I/art: Not late-enabling -Xcheck:jni (already on)
04-22 14:59:18.026 3355-3355/com.example.project W/System: ClassLoader referenced unknown path:
/data/app/com.example.project-2/lib/x86
04-22 14:59:18.045 3355-3355/com.example.project D/AndroidRuntime: Shutting down VM
04-22 14:59:18.046 3355-3355/com.example.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.project, PID: 3355
java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider:
java.lang.IllegalStateException:

******************************************************************************
* The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
* should follow the instructions here: *
* https://googlemobileadssdk.page.link/admob-android-update-manifest *
* to add a valid App ID inside the AndroidManifest. *
* Google Ad Manager publishers should follow instructions here: *
* https://googlemobileadssdk.page.link/ad-manager-android-update-manifest. *
******************************************************************************


at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException:

******************************************************************************
* The Google Mobile Ads SDK was initialized incorrectly. AdMob publishers *
* should follow the instructions here: *
* https://googlemobileadssdk.page.link/admob-android-update-manifest *
* to add a valid App ID inside the AndroidManifest. *
* Google Ad Manager publishers should follow instructions here: *
* https://googlemobileadssdk.page.link/ad-manager-android-update-manifest. *
******************************************************************************


at com.google.android.gms.internal.ads.zzyc.attachInfo(com.google.android.gms:play-services-ads-
lite@@19.1.0:33)
at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(com.google.android.gms:play-
services-ads-lite@@19.1.0:3)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
... 10 more
04-22 14:59:20.745 3355-3355/? I/Process: Sending signal. PID: 3355 SIG: 9

最佳答案

您调用了 startActivity 两次,也不需要 .setClass:

   public void send(View v){
String sign = ((EditText)(findViewById(R.id.editText_sign))).getText().toString();
Intent goToSecond = new Intent(this, SecondActivity.class);
//startActivity(goToSecond);//Comment this line
goToSecond.putExtra("sign", sign);
startActivity(goToSecond);
}

secondactivity 中,您在 setContentView 之前执行 findViewById,在 SecondActivity 中的 setContentView 之后添加以下行

webView = findViewById(R.id.webview);

您的异常表明存在空指针异常,因此请在代码中添加空检查:

String sign = "";
if (getIntent != null){
Intent caller = getIntent();
String sign = caller.getStringExtra("sign");
}

希望这有帮助

关于java - 不幸的是,项目已在 android studio 中停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362952/

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