0

I have developed one login form example using soap webservices.

In my app I have a start activity HomePage.HomePage and I have one button(myaccount). When I clicked myaccount button it is redirect to androidloginexampleactivity successfully.

Here I enter the details username and password. The details are correct, it means it is redirected to HomePage with pass the username. I got login is successful but after that it is force closed.

These is the my coding part:

Homepage.java is

public class HomePage extends Activity {
private Button account; 
private TextView tv; 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

Button account = (Button) findViewById(R.id.account);
TextView tv = (TextView) findViewById(R.id.textView1);
       tv.setText("Welcome ,"+getIntent().getExtras().getString("username"));

// Listening to register new account link
account.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
       Intent i = new Intent(getApplicationContext() , AndroidLoginExampleActivity.class);
       startActivity(i);
   }
}); 

  }

  }

AndroidLoginExampleActivity.java

  public class AndroidLoginExampleActivity extends Activity {
  private final String NAMESPACE = "http://ws.userlogin.com";
  private final String URL = "http://192.168.1.168:8085/Login/services/Login?wsdl";
  private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
  private final String METHOD_NAME = "authentication";
  /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button login = (Button) findViewById(R.id.btn_login);
    login.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
    loginAction();

    }
   });
  }

  private void loginAction(){
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    EditText userName = (EditText) findViewById(R.id.tf_userName);
    String user_Name = userName.getText().toString();
    EditText userPassword = (EditText) findViewById(R.id.tf_password);
    String user_Password = userPassword.getText().toString();

  //Pass value for userName variable of the web service
    PropertyInfo unameProp =new PropertyInfo();
    unameProp.setName("userName");//Define the variable name in the web service method
    unameProp.setValue(user_Name);//set value for userName variable
    unameProp.setType(String.class);//Define the type of the variable
    request.addProperty(unameProp);//Pass properties to the variable

  //Pass value for Password variable of the web service
    PropertyInfo passwordProp =new PropertyInfo();
    passwordProp.setName("password");
    passwordProp.setValue(user_Password);
    passwordProp.setType(String.class);
    request.addProperty(passwordProp);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try{
        androidHttpTransport.call(SOAP_ACTION, envelope);
           SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
           String status = response.toString();
           TextView result = (TextView) findViewById(R.id.tv_status);
           result.setText(response.toString());

           if(status.equals("Success!"))
            {
               Intent intent = new    Intent(AndroidLoginExampleActivity.this,HomePage.class);
               intent.putExtra("username",userName.getText().toString());
               startActivity(intent);

            }
           else
            {
                // Code for Login failure 
            }
           }



    catch(Exception e){

      }

      }

      }

How can i resolve dis error?

This is my logcat window:

07-17 15:52:00.997: E/AndroidRuntime(1798): FATAL EXCEPTION: main
07-17 15:52:00.997: E/AndroidRuntime(1798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidlogin.ws/com.androidlogin.ws.HomePage}: java.lang.NullPointerException
.
.
.
dalvik.system.NativeStart.main(Native Method)
 07-17 15:52:00.997: E/AndroidRuntime(1798): Caused by: java.lang.NullPointerException
 07-17 15:52:00.997: E/AndroidRuntime(1798):    at com.androidlogin.ws.HomePage.onCreate(HomePage.java:30)
oers
  • 18,436
  • 13
  • 66
  • 75
Krishna Veni
  • 2,217
  • 8
  • 27
  • 53
  • 3
    first of all dont post all your coding for your safety. just display where you are getting error. – Ram kiran Pachigolla Jul 17 '12 at 10:13
  • how you known it's force closed after login maybe it's crashing with `NetworkOnMainThreadException` ? so post full log when app is crashing. if it's crashing with `NetworkOnMainThreadException` then use asynctask or Handler to perform network operation in background – ρяσѕρєя K Jul 17 '12 at 10:13
  • you have to post where your getting error or what is ur problem, not like this.. – RajaReddy PolamReddy Jul 17 '12 at 10:14
  • you have posted 100s of lines of code, but you forgot to post the few lines log report.. bad. – Andro Selva Jul 17 '12 at 10:15
  • my first activity is HomePage.here i have one button(my account).i clicked that button means it is go to AndroidLoginExampleActivity.here if successful login details means it is redirect to HomePage and pass that username value also.but in my coding login details are successfully .afterthat app is force closed.how can i resolve dis error. – Krishna Veni Jul 17 '12 at 10:27
  • it doesn't help to repeat those sentences all over again – banzai86 Jul 17 '12 at 10:40
  • @Krishnadevi, Are you getting error after successful login and returning to HomePage another time?? Am I right? – Chintan Raghwani Jul 17 '12 at 11:06
  • s...now also am getting error only...how can i resolve dis error.please help me – Krishna Veni Jul 17 '12 at 11:08
  • just tell me your error? When it occurs? At first time on loading(going to) HomePage or at going Second time to HomePage? – Chintan Raghwani Jul 17 '12 at 11:09
  • And also tell me what is code At Line No 30 in your HomePage.java file? – Chintan Raghwani Jul 17 '12 at 11:12
  • now i run d above code without changes displayed force close messages. – Krishna Veni Jul 17 '12 at 11:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13989/discussion-between-krishnaveni-and-chintan-raghwani) – Krishna Veni Jul 17 '12 at 11:13
  • Its bad that you post all your code here. Please don't do that in future. For your general help in android we can pass values Using [Bundle](http://stackoverflow.com/questions/7537877/intent-and-bundle-relation). Follow this link asking the same question with having exact answer. Also double-click on your last fourth line and it ll take you to your code where yuor nullPointer exception is comming. – LuminiousAndroid Jul 17 '12 at 10:28

4 Answers4

1

Just check that your extra is not null:

if(getIntent().getExtras() != null) {
    tv.setText("Welcome ," + getIntent().getExtras().getString("username"));
}

Just add if() condition as I have stated in HomePage.Java

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
0

You've to start the AndroidLoginExampleActivity by calling startActivityForResult(Intent, int) instead of startActivity(). That way you may send data to the called activity and receive data from the called activity.

For your reference read - Activity.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

Your question: pass value from one activity to another activity + ?

My Answer: You can use SharedPreferences to pass values from one activity to an other.

To set a value:

String inputName = "Peter";
String inputPassword = "I love Stackoverflow";
SharedPreferences prefs = getSharedPreferences("myPrefs", 0);
SharedPreferences editor = prefs.edit();
editor.putString("accountName", inputName);
editor.putString("accountPassword", inputPassword);
editor.commit();

To get inputName and inputPassword in your other activities:

String prefOutputAccountName = "no name";
String prefOutputAccountPassword = "no password";
SharedPreferences prefs = getSharedPreferences("myPrefs", 0);
prefOutputAccountName = prefs.getString("accountName", prefOutputAccountName);
prefOutputAccountPassword = prefs.getString("accountPassword", prefOutputAccountPassword);

I hope i got your question right.

Carsten Drösser
  • 496
  • 1
  • 4
  • 16
0

Your question: pass value from one activity to another activity + ?

My Answer: You can use SharedPreferences to pass values from one activity to an other.

To set a value:

String inputName = "Peter";
String inputPassword = "I love Stackoverflow";
SharedPreferences prefs = getSharedPreferences("myPrefs", 0);
SharedPreferences editor = prefs.edit();
editor.putString("accountName", inputName);
editor.putString("accountPassword", inputPassword);
editor.commit();

To get inputName and inputPassword in your other activities:

String prefOutputAccountName = "no name";
String prefOutputAccountPassword = "no password";
SharedPreferences prefs = getSharedPreferences("myPrefs", 0);
prefOutputAccountName = prefs.getString("accountName", prefOutputAccountName);
prefOutputAccountPassword = prefs.getString("accountPassword", prefOutputAccountPassword);

I hope i got your question right.

zessx
  • 68,042
  • 28
  • 135
  • 158
sam
  • 56
  • 2