-3

I start on screen 'A'. in screen A, I navigate to screen 'B' where it change the orientation from portrait to landscape. If I now press the back button it get this exception.

It doesn't even reach the OnCreate Code

E/AndroidRuntime(518): FATAL EXCEPTION: main    
E/AndroidRuntime(518): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.coenen/be.coenen.TimeregistrationCardActivity}: java.lang.NullPointerException    
E/AndroidRuntime(518):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)    
E/AndroidRuntime(518):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)    
E/AndroidRuntime(518):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)    
E/AndroidRuntime(518):  at android.app.ActivityThread.access$2400(ActivityThread.java:125)    
E/AndroidRuntime(518):  at android.app.ActivityThread$H.      handleMessage(ActivityThread.java:2037)    
E/AndroidRuntime(518):  at android.os.Handler.dispatchMessage(Handler.java:99)    
E/AndroidRuntime(518):  at android.os.Looper.loop(Looper.java:123)    
E/AndroidRuntime(518):  at android.app.ActivityThread.main(ActivityThread.java:4627)    
E/AndroidRuntime(518):  at java.lang.reflect.Method.invokeNative(Native Method)    
E/AndroidRuntime(518):  at java.lang.reflect.Method.invoke(Method.java:521)    
E/AndroidRuntime(518):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)    
E/AndroidRuntime(518):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)    
E/AndroidRuntime(518):  at dalvik.system.NativeStart.main(Native Method)    
E/AndroidRuntime(518): Caused by: java.lang.NullPointerException    
E/AndroidRuntime(518): at be.coenen.TimeregistrationCardActivity.onCreateDialog(TimeregistrationCardActivity.java:314)    
E/AndroidRuntime(518):  at android.app.Activity.onCreateDialog(Activity.java:2472)    
E/AndroidRuntime(518):  at android.app.Activity.createDialog(Activity.java:881)    
E/AndroidRuntime(518):  at android.app.Activity.restoreManagedDialogs(Activity.java:870)    
E/AndroidRuntime(518):  at android.app.Activity.performRestoreInstanceState(Activity.java:816)    
E/AndroidRuntime(518):  at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)    
E/AndroidRuntime(518):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)

` OnCreate screen A:

public class TimeregistrationOverviewActivity extends Activity {
/** Called when the activity is first created. */
private Context context;
private ArrayList<TimeregistrationOverview> listTimeRegOverView;
private TimeregistrationOverviewAdapter listViewAdapter;
private ListView listView;
private String emp, dateTimeRegistration;
private TextView txtDateDisplay;
private Button btnPickDate, btnInterventionRapport;
private int intYear, intMonth, intDay, arrayCount;
private Intent i = new Intent();
final Calendar c = Calendar.getInstance();
final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    try{

    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.main);


    listView = (ListView) findViewById(R.id.android_TOlist1);
    txtDateDisplay = (TextView) findViewById(R.id.TOdateDisplay);
    btnPickDate = (Button) findViewById(R.id.TObtnDatum);
    btnPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    intYear = c.get(Calendar.YEAR);
    intMonth = c.get(Calendar.MONTH);
    intDay = c.get(Calendar.DAY_OF_MONTH);
    // Get Employee ID
    i = getIntent();
    emp = i.getExtras().getString("Employee");
    if (!(i.getExtras().getString("Date") == null)) {
        dateTimeRegistration = i.getExtras().getString("Date");

    }

    // display the current date
    updateDisplay();

    // gegevens ophalen/updaten
    new DownloadData().execute(LogInActivity.SERVICE_URI
            + "GetTimeregistrationOverview?Date=" + dateTimeRegistration
            + "&Employee=" + emp);
} catch (Exception e) {
    ShowError();
}
}

OnCreate screen B:

public class TimeregistrationCardActivity extends Activity {
/** Called when the activity is first created. */
private TimeregistratrationCard timeRegistrationCard;
private Intent intent = new Intent();
private int id, minute, hour, minuteNow, hourNow;
private TextView date, customerName, description, taskDocNo, activity,
        externalInfo, internalInfo, percentageComplete, project, startTime,
        stopTime;
private CheckBox coaching;
private String employeeNo, dateTimeregistration;
private ProgressDialog m_ProgressDialog;
private Calendar c = Calendar.getInstance();

static final int TIME_DIALOG_ID = 0, TIME_DIALOG_ID2 = 1,
        DATE_DIALOG_ID = 0;

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

    m_ProgressDialog = ProgressDialog.show(
            TimeregistrationCardActivity.this, "", getResources()
                    .getString(R.string.ProgressDialog), true);

    date = (TextView) findViewById(R.id.TRKtxtDatum);
    customerName = (TextView) findViewById(R.id.TRKtxtKlantNaam);
    description = (TextView) findViewById(R.id.TRKtxtDescription);
    taskDocNo = (TextView) findViewById(R.id.TRKtxtTaskDocNo);
    activity = (TextView) findViewById(R.id.TRKtxtActivity);
    coaching = (CheckBox) findViewById(R.id.TRKchkCoaching);
    externalInfo = (TextView) findViewById(R.id.TRKtxtExternalInformation);
    internalInfo = (TextView) findViewById(R.id.TRKtxtInternalInformation);
    percentageComplete = (TextView) findViewById(R.id.TRKtxtPercentageComplete);
    project = (TextView) findViewById(R.id.TRKtxtProject);
    startTime = (TextView) findViewById(R.id.TRKtxtStartTime);
    stopTime = (TextView) findViewById(R.id.TRKtxtStopTime);

    intent = getIntent();
    id = intent.getExtras().getInt("ID");
    employeeNo = intent.getExtras().getString("Emp");
    dateTimeregistration = intent.getExtras().getString("Date");
    new DownloadData().execute(LogInActivity.SERVICE_URI
            + "GetTimeregistrationCard?ID=" + id);

}
user1264255
  • 305
  • 1
  • 5
  • 13
  • 1
    Tip: 99% of all NullPointerExceptions are caused by something accidentally being null, which is easily fixed about 99% of the time. – keyser May 22 '12 at 15:25
  • In general it will help if you post the code in question, and the error stack trace. For instance, if you had posted your code we might be able to help you figure out why one of your values is null – FoamyGuy May 22 '12 at 15:28
  • Was the both screens extending seperate `Activity` – Praveenkumar May 23 '12 at 04:32
  • I just press the back button on the android emulator. I haven't override this method so how can I post code? – user1264255 May 23 '12 at 06:53
  • @Keyser if it was so easy to fix, I wouldn't asked it here. Just pressing back button, so i don't set anything on null. just standard android back button. – user1264255 May 23 '12 at 06:56
  • 1
    Then, do whatever you need as per in [this](http://stackoverflow.com/a/5726776/940096) answer, in your both activities. It helps you lot. – Praveenkumar May 23 '12 at 06:58
  • @user1264255 I don't know you, but I know 99% of all people would (which is why I posted in the first place) – keyser May 23 '12 at 08:24

2 Answers2

3

look at this line in your code, whatever is there is null when returning to the activity

E/AndroidRuntime(518): Caused by: java.lang.NullPointerException

E/AndroidRuntime(518): at be.coenen.TimeregistrationCardActivity.onCreateDialog(TimeregistrationCardActivity.java:314)
tyczj
  • 71,600
  • 54
  • 194
  • 296
1

Use onConfigurationChanged method in your both activity with below code

public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

Instead of Toast Just use start the activity to which activity you want to opens. I mean, in Screen A's onConfigurationChanged method, its changing landscape from portrait means, just start the Screen A instead of Toast there.

Don't forgot to add below code in your activity's manifest file -

<activity android:name=".your Activity name"  // Your activity name.
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

Hope this helps you.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173