0

The code is all about taking the zodiac sign as the input in the 1st activity and when clicked OK navigates to the next activity where it displays the details as the web view of that particular zodiac sign. When ever I try to run the code in the emulator or in my mobile I get a warning project is continuously stopping and the application closes.

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>

Logcat:

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

1 Answers1

0

You are calling startActivity twice, also .setClass is not needed:

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

In secondactivity you are doing findViewById before setContentView add the below line after setContentView in SecondActivity

webView = findViewById(R.id.webview);

Your Exception says there is a nullpointer exception so add a null check to your code:

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

Hope this helps

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • facing the same problem even. – Pradyumna Shashank Apr 23 '20 at 07:13
  • @PradyumnaShashank are you seeing same error log? Can you update the error log? – keshav kowshik Apr 23 '20 at 07:15
  • @PradyumnaShashank your log in question details about ads but in your code you have not used add any where have you added that in gradle? – keshav kowshik Apr 23 '20 at 07:19
  • * daemon not running; starting now at tcp:5037 * daemon started successfully Gradle sync started Gradle sync finished in 1 s 676 ms (from cached state) Executing tasks: [:app:assembleDebug] in project C:\Users\Pradyumna\AndroidStudioProjects\project Emulator: emulator: WARNING: EmulatorService.cpp:448: Cannot find certfile: C:\Users\Pradyumna\.android\emulator-grpc.cer security will be disabled. Emulator: Started GRPC server at 127.0.0.1:8554 Gradle build finished in 23 s 235 ms Install successfully finished in 3 s 926 ms. Emulator: Process finished with exit code 0 – Pradyumna Shashank Apr 23 '20 at 12:41
  • this is the log which is being seen after making the update you have mentioned. @Keshav1234 – Pradyumna Shashank Apr 23 '20 at 12:43
  • @PradyumnaShashank this does not specify anything, once the installation is finished you can see an error in logcat, you can filter the logcat to show only the exeception. If you add that log I can tell what might be the issue. – keshav kowshik Apr 23 '20 at 17:14
  • 04-23 21:07:44.282 3842-3842/com.example.project E/SysUtils: ApplicationContext is null in ApplicationStatus – Pradyumna Shashank Apr 24 '20 at 12:14
  • 04-23 21:07:44.297 3842-3842/com.example.project E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY) 04-23 21:07:44.297 3842-3842/com.example.project E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY) – Pradyumna Shashank Apr 24 '20 at 12:14
  • 04-23 21:07:44.391 3842-3842/com.example.project E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp – Pradyumna Shashank Apr 24 '20 at 12:15
  • 04-23 21:07:44.419 3842-3842/com.example.project E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.project, PID: 3842 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference ActivityThread.performLaunchActivity(ActivityThread.java:2416) ActivityThread.handleLaunchActivity(ActivityThread.java:2476) ActivityThread.-wrap11(ActivityThread.java) – Pradyumna Shashank Apr 24 '20 at 12:23
  • ActivityThread$H.handleMessage(ActivityThread.java:1344) Handler.dispatchMessage(Handler.java:102)Looper.loop(Looper.java:148)ActivityThread.main(ActivityThread.java:5417)java.lang.reflect.Method.invoke(Native Method) – Pradyumna Shashank Apr 24 '20 at 12:24
  • com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference com.example.project.SecondActivity.onCreate(SecondActivity.java:22)Activity.performCreate(Activity.java:6237).Instrumentation.callActivityOnCreate(Instrumentation.java:1107) – Pradyumna Shashank Apr 24 '20 at 12:24
  • .ActivityThread.performLaunchActivity(ActivityThread.java:2369) .ActivityThread.handleLaunchActivity(ActivityThread.java:2476) ActivityThread.-wrap11(ActivityThread.java).ActivityThread$H.handleMessage(ActivityThread.java:1344)Handler.dispatchMessage(Handler.java:102)Looper.loop(Looper.java:148) atActivityThread.main(ActivityThread.java:5417)at java.lang.reflect.Method.invoke(Native Method) – Pradyumna Shashank Apr 24 '20 at 12:24
  • these are logcat errors @Keshav1234 – Pradyumna Shashank Apr 24 '20 at 12:25
  • @PradyumnaShashank check my edited answer to fix you null pointer exception. If my answer helped you please accept it as answer and upvote it. – keshav kowshik Apr 24 '20 at 17:05
  • thank you it has help me :) – Pradyumna Shashank Apr 25 '20 at 08:33