从纵向切换到横向时出错
Error when switching from Portrait to Landscape
我的 Android 应用程序很简单。它只有 1 activity。我为同一个 activity 创建了两个布局:一个用于纵向位置(在 res/layout 文件夹内),一个用于横向位置(在 文件夹内) =]res/layout-land文件夹)。我在这个问题的末尾给出了两者的代码。
我在 myActivity.java 文件中没有什么特别的,我只是膨胀布局:
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);
}
}
此时一切正常。如果我将我的设备保持在纵向位置,应用程序将调用适当的 XML。它也很有魅力,以防我决定将它横向放置。
当我决定向上述 myActivity.java 文件中添加一点代码时,问题就出现了;还是很简单的!
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!!
}
});
}
}
执行那段代码后,如果我决定横向显示,应用程序将突然停止并显示 "Unfortunately myApp has stopped"。有趣的是,如果我不实现 onClickListener,就不会发生这种情况!
LogCat给出的准确错误如图:
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
不要认为布局 XML 文件对我的问题有多大作用,但我会将它们复制并粘贴到 this link (for portrait) and this other link (for landscape) 中,以免使这个问题过长。
提前致谢!
在横向 XML 文件中,ImageButton 名为 imageButton_Users 而不是 imageButton_Pacientes.重命名它,一切都会正常工作。
您正在尝试查找不存在的视图,因此您的应用程序将会崩溃。
我的 Android 应用程序很简单。它只有 1 activity。我为同一个 activity 创建了两个布局:一个用于纵向位置(在 res/layout 文件夹内),一个用于横向位置(在 文件夹内) =]res/layout-land文件夹)。我在这个问题的末尾给出了两者的代码。
我在 myActivity.java 文件中没有什么特别的,我只是膨胀布局:
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);
}
}
此时一切正常。如果我将我的设备保持在纵向位置,应用程序将调用适当的 XML。它也很有魅力,以防我决定将它横向放置。
当我决定向上述 myActivity.java 文件中添加一点代码时,问题就出现了;还是很简单的!
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!!
}
});
}
}
执行那段代码后,如果我决定横向显示,应用程序将突然停止并显示 "Unfortunately myApp has stopped"。有趣的是,如果我不实现 onClickListener,就不会发生这种情况!
LogCat给出的准确错误如图:
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
不要认为布局 XML 文件对我的问题有多大作用,但我会将它们复制并粘贴到 this link (for portrait) and this other link (for landscape) 中,以免使这个问题过长。
提前致谢!
在横向 XML 文件中,ImageButton 名为 imageButton_Users 而不是 imageButton_Pacientes.重命名它,一切都会正常工作。
您正在尝试查找不存在的视图,因此您的应用程序将会崩溃。