0
votes

I'm getting the errors below whenever I click the button to launch the next activity. The app is suppose to launch the activity where the user can create an account and store data to SQL Server through API.

Here are the errors:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.durban.labapp_unisa/com.durban.labapp_unisa.CreateUserActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.durban.labapp_unisa.CreateUserActivity.onCreate(CreateUserActivity.java:44) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 more

My main activity is:

public class MainActivity extends Activity implements OnClickListener {

Button btnLogin, btnRegister, btnDisplay;

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

  //Initialize the layout components
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnRegister = (Button) findViewById(R.id.btnRegister);

  //set onClick listeners for buttons
    btnLogin.setOnClickListener(this);
    btnRegister.setOnClickListener(this);
}

@Override  
public void onClick(View v) {

    switch(v.getId()){

    case R.id.btnRegister:
        Intent cr = new Intent(getApplicationContext(), CreateUserActivity.class);
        startActivity(cr);
        break;
    }
}

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

}

And the other activity is:

/*
* This activity used to create users. 
* When user is created it will redirect to LoginActivity.
*/
public class CreateUserActivity extends Activity {

EditText StudentNr, Surname, Fullname, Password,Cell, Qual;
Button Save, backButton;

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

// Show the Up button in the action bar.
  setupActionBar();

//setup button listener
  StudentNr = (EditText) findViewById(R.id.etStudentNr);
  Surname = (EditText) findViewById(R.id.etSurname);
  Fullname = (EditText) findViewById(R.id.etFullname);
  Password = (EditText) findViewById(R.id.etPass1);
  Cell = (EditText) findViewById(R.id.etCell);
  Qual = (EditText) findViewById(R.id.etQua);
  Save = (Button) findViewById(R.id.btnRegister); 
  backButton = (Button) findViewById(R.id.btnBack1);

  Save.setOnClickListener(new View.OnClickListener() {        
      @Override
      public void onClick(View v) {
            // TODO Auto-generated method stub

            String studentNr, surname, fullname, password, cell, qual;

            studentNr = StudentNr.getText().toString();
            surname = Surname.getText().toString();
            fullname = Fullname.getText().toString();
            password = Password.getText().toString();
            cell = Cell.getText().toString();
            qual = Qual.getText().toString();

            UserDetailsTable Records = new UserDetailsTable(studentNr,surname, fullname, password, cell, qual);

            new AsyncCreateUser().execute(Records);
      }
  });   

  backButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          finish();
    }
  });

}

protected class AsyncCreateUser extends AsyncTask<UserDetailsTable, Void, Void>{

    @Override
    protected Void doInBackground(UserDetailsTable... params){

        RestAPI api = new RestAPI();
        try{

            api.CreateNewAccount(params[0].getStudentNr(), params[1].getSurname(), params[2].getFullname(), params[3].getPassword(), params[4].getCellNr(), params[5].getQual());

        }catch(Exception e){
            Log.d("AsyncCreateUser", e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result){

        //launch the login page after account successfully created
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(i);
    }
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
      }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

      switch (item.getItemId()) {
      case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
      }
      return super.onOptionsItemSelected(item);
}

The manifest looks like:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.durban.labapp_unisa.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.durban.labapp_unisa.CreateUserActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:stateNotNeeded="true">
        <intent-filter >
            <action android:name="android.intent.action.CREATEUSERACTIVITY"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity> 

    <activity
        android:name="com.durban.labapp_unisa.JSONParser"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.JSONPARSER"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>

    <activity
        android:name="com.durban.labapp_unisa.RestAPI"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.RESTAPI"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>

    <activity
        android:name="com.durban.labapp_unisa.UserDetailsTable"
        android:label="@string/app_name">
        <intent-filter >
            <action android:name="android.intent.action.USERDETAILSTABLE"/>
            <action android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
     </activity>
</application>

Please assist me with this

1
getApplicationContext() to class name.this I think this is the problemRakshit Nawani

1 Answers

0
votes

Try to change the below code

    Intent cr = new Intent(getApplicationContext(), CreateUserActivity.class);
    startActivity(cr);

To

    Intent cr = new Intent(MainActivity.this, CreateUserActivity.class);
    startActivity(cr);

I think the error is in that line...