Reputation: 5
I have two main.xml files in layout-land and layout-port folders and when the orientation of the phone changes it forces a reload of main.xml.
@Override
public void onConfigurationChanged(Configuration configure){
super.onConfigurationChanged(configure);
setContentView(R.layout.main);
It works fine in portrait but pressing any of the imagebuttons on the page in landscape causes the app to crash. The imagebuttons all change an image called grid to a different image when pressed. The buttons that open another page work fine in both layouts. Here's a sample of the code.
final ImageView imageView = (ImageView)findViewById(R.id.grid);
// This is an imagebutton
findViewById(R.id.plank).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.gridplanks);
}
});
// This is a button that brings up a new page
findViewById(R.id.mcg).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent("com.example.minecraftcraftingguide.MCG"));
}
});
Is this what you need? New to programming, sorry.
>09-18 22:20:46.688: ERROR/AndroidRuntime(334): FATAL EXCEPTION: main
09-18 22:20:46.688: ERROR/AndroidRuntime(334): java.lang.NullPointerException
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at com.example.minecraftcraftingguide.Menu$1.onClick(Menu.java:27)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.view.View.performClick(View.java:2485)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.view.View$PerformClick.run(View.java:9080)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.os.Handler.handleCallback(Handler.java:587)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-18 22:20:46.688: ERROR/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 3918
Reputation: 747
Do you have a main.xml in your default layout folder or just the two in layout-port and layout-land?
If I were you, I would move the file from layout-port to your default layout folder. Then the app will use the file in layout-land if the app is in landscape folder, and otherwise (if the app is in portrait mode) will use the file in your default folder.
Another hint: Check the spelling in both xml files. Maybe you are missing a character in one of the button id's.
Upvotes: 1
Reputation: 1015
You have to decalare ConfigChanges in mainfest, and you dont need to write the setcontentView() in onOnfigurationChanged() method, infact, after declaring in mainfest , you dont need to write anything in your program, the android will automatically get the landscape xml file which resides in layout-land xml file.
Because whenever you call setContentView(), you have to declare its elements again.!!!
Just mention in the mainfest, in this activity tag android:configChanges=keyboardHidden|orientation or Dont write anything in your onConfigChanged() method
Upvotes: 0