My Android app is simple. It has only 1 activity. I created two layouts for the same activity: one for the portrait position (inside the res/layout folder) and one for the landscape position (inside the res/layout-land folder). I give the code for both at the end of this question.
I have nothing special inside the myActivity.java file, I just inflate the layout:
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
public class MCentralActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
}
}
Everything works fine at this point. If I hold my device in the portrait position, the app will call the appropriate XML. It works like a charm too, in case I decide to hold it in the landscape position.
The problem arises when I decide to add a little bit of code to the aforementioned myActivity.java file; it is still really simple!
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MCentralActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton ibPacientes;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
ibPacientes = (ImageButton)findViewById(R.id.imageButton_Pacientes);
ibPacientes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//NOTHING INSIDE HERE!!
}
});
}
}
After implementing that little code, if I decide to go landscape, the App will stop abruptly saying "Unfortunately myApp has stopped". Interesting enough this won't occur if I don't implement onClickListener!
The exact error given by LogCat is as shown:
03-28 13:39:27.870: E/SurfaceFlinger(157): DRAW orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: E/SurfaceFlinger(157): STATE orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: I/SurfaceFlinger(157): @@@@@@@@ orientation:1, transformOri:4
Don't think the layout XML files has to do much in my problem, but I will copy and paste them in this link (for portrait) and this other link (for landscape), in order to not to make this question excessively long.
Thanks in advance!
In your landscape XML file you have the ImageButton called imageButton_Users instead of imageButton_Pacientes. Rename it and everything should work fine.
You're trying to find a view that's not there and so your app will crash.