F1sher
F1sher

Reputation: 7310

Android: startActivityForResult crashes during starting new activity

I am new Android coder and I'm fighting with function startActivityForResult. When I click Button that starts this method I can't go inside another activity because it crashes:

So there is MainActivity.java class. Here in public void setRegisterOnClick(View v) I want to use startActivityForResult twice, sending other intents to the same activity. Intents contain int number which is delivered to Users.class and used to declare giveResponse. But each time I click button launching startActivityForResult my app crashes.

package com.example.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int USERS_REQUEST_CODE=1;
    private static String sActualLogin = "admin";
    private static String sActualPassword = "qwerty";
    private static String sContainer = "";

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void setLoginOnClick(View v)
    {
        EditText etLogin = (EditText) findViewById(R.id.tfLogin);
        EditText etPassword = (EditText) findViewById(R.id.tfPassword);

        String sLogin = etLogin.getText().toString();
        String sPassword = etPassword.getText().toString();

        if (sLogin.equals(sActualLogin) && sPassword.equals(sActualPassword))
        {
            Intent intent = new Intent (this, Inside.class);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(this, "Invalid username or password.", Toast.LENGTH_LONG).show();
        }

    }

    public void setRegisterOnClick(View v)
    {
        Intent intentLOGIN = new Intent(this, Users.class);
        intentLOGIN.putExtra("Type",1);
        startActivityForResult(intentLOGIN, USERS_REQUEST_CODE);
        sActualLogin=sContainer;

        Intent intentPASSWORD= new Intent(this, Users.class);
        intentPASSWORD.putExtra("Type",2);
        startActivityForResult(intentPASSWORD, USERS_REQUEST_CODE);
        sActualPassword=sContainer;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            sContainer = data.getStringExtra(Users.RESPONSE);
        }    
}

Users.class

package com.example.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Users extends Activity {

    public static final String RESPONSE = "Response";

    EditText etNewLogin = (EditText) findViewById(R.id.etChangeLogin);
    EditText etNewPassword = (EditText) findViewById(R.id.etChangePassword);

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

    public void giveResponse(View v)
    {   
        Intent intent = getIntent();
        int iOperation = intent.getIntExtra("Type", 0);


        if (iOperation == 1){
             String sNewLogin = etNewLogin.getText().toString();    
             Intent resultIntent1 = new Intent();
             resultIntent1.putExtra(RESPONSE, sNewLogin); 
             setResult(RESULT_OK, resultIntent1);
             finish();
        }

        if (iOperation == 2){
             String sNewPassword = etNewPassword.getText().toString();
             Intent resultIntent2 = new Intent();
             resultIntent2.putExtra(RESPONSE, sNewPassword);    
             setResult(RESULT_OK, resultIntent2);
             finish();
    }


    }
}

Fragment of manifest:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.example.login.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name="com.example.login.Users"></activity>
        <activity android:name="com.example.login.Inside"></activity>

    </application>

Errors:

05-12 20:22:10.350: W/dalvikvm(6349): threadid=1: thread exiting with uncaught exception (group=0x40a191f8)
05-12 20:22:10.360: E/AndroidRuntime(6349): FATAL EXCEPTION: main
05-12 20:22:10.360: E/AndroidRuntime(6349): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.login/com.example.login.Users}: java.lang.NullPointerException
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.os.Looper.loop(Looper.java:137)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread.main(ActivityThread.java:4429)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at dalvik.system.NativeStart.main(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349): Caused by: java.lang.NullPointerException
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.Activity.findViewById(Activity.java:1794)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at com.example.login.Users.<init>(Users.java:13)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at java.lang.Class.newInstanceImpl(Native Method)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at java.lang.Class.newInstance(Class.java:1319)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-12 20:22:10.360: E/AndroidRuntime(6349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-12 20:22:10.360: E/AndroidRuntime(6349):     ... 11 more

Well I wrote a lot for such trivial problem (I guess :P) but as I am unexperienced it takes hours to fights with it and I stil don't get it. Please guide/help me.

Upvotes: 0

Views: 5891

Answers (3)

Chris Klingler
Chris Klingler

Reputation: 5296

Often this happens when you haven't declared an activity correctly in your AndroidManifest file, make sure the directory info is correct so your app can find the activity you are trying to get to!

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

EditText etNewLogin;
EditText etNewPassword;;  
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.users);
    etNewLogin = (EditText) findViewById(R.id.etChangeLogin);
    etNewPassword = (EditText) findViewById(R.id.etChangePassword);    
}

You need to find the id after setting the content to the activity.

Also in setLoginOnClick of MainActivity

    Intent intent = new Intent (MainActivity.this, Inside.class);// use activity context
    startActivity(intent);

Same for

    Toast.makeText(MainActivit.this, "Invalid username or password.", Toast.LENGTH_LONG).show();
    Intent intentLOGIN = new Intent(MainActivity.this, Users.class);// use activity context
    intentLOGIN.putExtra("Type",1);
    startActivityForResult(intentLOGIN, USERS_REQUEST_CODE);
    sActualLogin=sContainer;

    Intent intentPASSWORD= new Intent(MainActivity.this, Users.class);// use activity context
    intentPASSWORD.putExtra("Type",2);
    startActivityForResult(intentPASSWORD, USERS_REQUEST_CODE);
    sActualPassword=sContainer;

Upvotes: 1

stinepike
stinepike

Reputation: 54672

you must set the layout using setContentView before accessing the child widget using findViewByID

for example

private EditText etNewLogin;
private EditText etNewPassword;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.users);
    tNewLogin = (EditText) findViewById(R.id.etChangeLogin);
    tNewPassword = (EditText) findViewById(R.id.etChangePassword);
}

Also make sure that you are accesing the view using correct id

Upvotes: 2

Related Questions