-1

My classmates and I are working on a recipe app and we are running into errors. We can't start a second activity with intents. Any suggestions ?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.loginpage">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Foodly"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".RecipeList"
        android:label="@string/title_activity_recipes_list"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <activity android:name=".WelcomeActivity" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


Java Code Activity 1

public class MainActivity extends AppCompatActivity {
    private EditText name;
    private EditText passwd;


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




    public void onButtonClick(View v) {


        name = (EditText)findViewById(R.id.editText2);
        passwd = (EditText)findViewById(R.id.editText1);
        String wrong = "Wrong Credentials";
        String userinput = name.getText().toString();
        String userinput2 = passwd.getText().toString();
        //boolean t = LoginManager.isKnownUser(userinput,userinput2);

        if (LoginManager.isKnownUser(userinput, userinput2)) {

            Intent intent = new Intent(this, WelcomeActivity.class);
            startActivity(intent);

        } else {
            Toast.makeText(getApplicationContext(), wrong, Toast.LENGTH_LONG).show();
        }
    }


}

Login Manager

package com.example.loginpage;



public class LoginManager {

    public static boolean isKnownUser(String name, String passwd) {
        if (name.equals("Otoi") && passwd.equals("1234")) {
            return true;
        } else {
            return false;
        }
    }

}

E / AndroidRuntime: FATAL EXCEPTION: main Process: com.example.loginpage, PID: 3091 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ com.example.loginpage / com.example.loginpage.WelcomeActivity }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2843) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java: 78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java: 108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java: 68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1808) at android.os.Handler.dispatchMessage(Handler.java: 106) at android.os.Looper.loop(Looper.java: 193) at android.app.ActivityThread.main(ActivityThread.java: 6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java: 493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java: 142) at android.content.ComponentName.< init(ComponentName.java: 130) at android.content.Intent.< init(Intent.java: 6082) at com.example.loginpage.WelcomeActivity.< init(WelcomeActivity.java: 10) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java: 69) at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java: 43) at android.app.Instrumentation.newActivity(Instrumentation.java: 1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2831) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 3048)

at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java: 78)

at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java: 108)

at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java: 68)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1808) at android.os.Handler.dispatchMessage(Handler.java: 106) at android.os.Looper.loop(Looper.java: 193) at android.app.ActivityThread.main(ActivityThread.java: 6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java: 493)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 858)

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

1

Might need to amend the AndroidManifest.xml as so:

<application
    android:allowBackup="true"
    android:name="org.mydomain.mycompany.MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="Foodly"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".WelcomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

    <activity
        android:name=".RecipeList"
        android:label="@string/title_activity_recipes_list"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

</application>

You will want to name the Application Object name to the fully qualified path name (to your MyApplication.java file), and, I add the intent-filter to android.intent.action.MAIN to all of my actions, not just the LAUNCHER activity. I also fully qualify the name of the LAUNCHER activity, and the rest can use the .WelcomeActivity naming convention.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • there is no `Application` class involved - while those fully qualified names might serve no real purpose; that `MainActivity` is just alike the others - except the `intent-filter` (I've stopped my edit at some point). – Martin Zeitler May 23 '19 at 04:47