关于一个在竖屏上做到游戏在横屏板子上崩了的问题 本帖最后由 snowraindy 于 2010-09-07 11:11:27 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 记得前面有人问过这个问题,在切换横竖屏的时候,都会重新调用Activity的onCreate()函数,所以如果你在onCreate()定义的变量都会改变,把你需要的变量保存起来,在onCreate()调用变量至于具体的问题要看你到底是出什么错决定 直接是一个jni的异常,贴出来logcat也没有具体什么显示 我这是在oncreate下写的,请问2楼所说的把需要的变量保存起来,在onCreate()调用变量怎么实现protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // fullscreen mode requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); final TextView tv = new TextView(this); tv.setText("Initializing"); File sourcepath=new File("/sdcard/" + Globals.ApplicationName); if(!sourcepath.exists()) { setContentView(R.layout.main); //获得按钮和文本对象 m_TextView=(TextView)findViewById(R.id.m_text); m_Button1=(Button)findViewById(R.id.m_button1); m_TextView1=(TextView)findViewById(R.id.m_text1); m_Button2=(Button)findViewById(R.id.m_button2); m_TextView2=(TextView)findViewById(R.id.m_text2); m_Button1.setWidth(150); m_Button2.setWidth(150); m_TextView.setTextSize(20); m_Button1.setTextSize(20); m_TextView1.setTextSize(20); m_Button2.setTextSize(20); m_TextView2.setTextSize(20); m_TextView.setText("您的SD卡上没有所需要的资源请选择操作:"); m_Button1.setText("点击下载"); m_TextView1.setText("有联网的用户可以选择从网上直接下载资源。"); m_Button2.setText("退出程序"); m_TextView2.setText("无联网的用户请把zhaocha文件夹拷贝到SD卡的根目录,请拷贝完后再次进入游戏,请点击退出程序。"); //设置按钮的事件监听 m_Button1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub //处理事件 setContentView(tv); downloader = new DataDownloader(this,ZhaochaActivity.this, tv); } }); m_Button2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub //处理事件 ZhaochaActivity.this.finish(); } }); } else{downloader = new DataDownloader(this, tv);} } 另外问下有好多单步调试jni的工具吗 楼主需用 void onSaveInstanceState(Bundle outState) 我用这个函数也不行吗?public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig); } 方法一:manifest中增加android:configChanges="orietation"属性。然后在代码中响应onConfigurationChanged和onSaveInstanceState事件,并且在onCreate中处理savedInstanceState。方法二:小心处理onCreate,保证重新运行时,相关变量都重置了,每次运行onCreate都和第一次运行onCreate一样。个人推荐方法二。 恩 , 横竖屏切换 , Activity会重启。 谢谢大家,我按9楼的方法设置了监听方法,但是没有在onCreate中处理savedInstanceState。我看网上写的只有设置监听的方法,我设置了以后发现,横屏依然会崩。能不能给个方法2的实例看看呢,这样好理解。 更奇妙的是这样的,我最开始设定的是一只竖屏属性android:screenOrientation="portrait"在横竖屏上都可以显示并且正确,但我设置了监听方法后去掉了android:screenOrientation="portrait",然后在竖屏的时候显示正确,并且在这个时候我切换到横屏没问题,还是一样可以玩。但是如果我一开始是横屏的话,就马上崩了。 我12楼说错了点 我处理了savedInstanceState但没用void onSaveInstanceState(Bundle outState) private String mSomeState;void onCreate(Bundle savedInstanceState){ if(savedInstanceState != null) { mSomeState = savedInstanceState.getString("name"); }}void onSaveInstanceState(Bundle outState){ outState.putString("name","myTestState"); }大概是这么用的(直接回复时敲上去的难免有写错的地方) 那protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // fullscreen mode requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); TextView tv = new TextView(this); tv.setText("Initializing"); setContentView(tv); downloader = new DataDownloader(this, tv); }TextView文本变量也要保存吗?downloader调用的对象也要保存? create以后的函数不保存有关系吗?比如说 public void initSDL() { mLoadLibraryStub = new LoadLibrary(); mAudioThread = new AudioThread(this); mGLView = new DemoGLSurfaceView(this); setContentView(mGLView); // Receive keyboard events mGLView.setFocusableInTouchMode(true); mGLView.setFocusable(true); mGLView.requestFocus(); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, Globals.ApplicationName); wakeLock.acquire(); } 我之前也遇到过类似的问题,解决方法很简单嘛,就是在OnDestroy()里面把你所有的成员变量都关掉且置为空。这种问题一般都是资源清理有问题造成的。 android解析json报空指针异常。 调用android输入法服务,获取字符问题? 未联网的电脑,如何安装Android开发环境? android omap平台内核代码的编译问题 ,在线等 关于sharedpreference问题求助 请教高手,写了一个ANDROID简单程序,但结果显示却不对 求指导!如何弹出对话框? 在eclipse中,如何让主工程替换library工程的src文件 关于用LocationManager获得自身的经纬度 android studio cmake 编译第三方源码,提示h文件找不到 如何知道一个程序被打开或被关闭了?? Android出错
至于具体的问题要看你到底是出什么错决定
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final TextView tv = new TextView(this);
tv.setText("Initializing");
File sourcepath=new File("/sdcard/" + Globals.ApplicationName);
if(!sourcepath.exists())
{
setContentView(R.layout.main);
//获得按钮和文本对象
m_TextView=(TextView)findViewById(R.id.m_text);
m_Button1=(Button)findViewById(R.id.m_button1);
m_TextView1=(TextView)findViewById(R.id.m_text1);
m_Button2=(Button)findViewById(R.id.m_button2);
m_TextView2=(TextView)findViewById(R.id.m_text2);
m_Button1.setWidth(150);
m_Button2.setWidth(150);
m_TextView.setTextSize(20);
m_Button1.setTextSize(20);
m_TextView1.setTextSize(20);
m_Button2.setTextSize(20);
m_TextView2.setTextSize(20);
m_TextView.setText("您的SD卡上没有所需要的资源请选择操作:");
m_Button1.setText("点击下载");
m_TextView1.setText("有联网的用户可以选择从网上直接下载资源。");
m_Button2.setText("退出程序");
m_TextView2.setText("无联网的用户请把zhaocha文件夹拷贝到SD卡的根目录,请拷贝完后再次进入游戏,请点击退出程序。");
//设置按钮的事件监听
m_Button1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View arg0) {
// TODO Auto-generated method stub
//处理事件
setContentView(tv);
downloader = new DataDownloader(this,ZhaochaActivity.this, tv);
}
});
m_Button2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View arg0) {
// TODO Auto-generated method stub
//处理事件
ZhaochaActivity.this.finish();
}
});
}
else{downloader = new DataDownloader(this, tv);}
}
public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig);
}
方法二:小心处理onCreate,保证重新运行时,相关变量都重置了,每次运行onCreate都和第一次运行onCreate一样。个人推荐方法二。
能不能给个方法2的实例看看呢,这样好理解。
{
if(savedInstanceState != null)
{
mSomeState = savedInstanceState.getString("name");
}
}void onSaveInstanceState(Bundle outState)
{
outState.putString("name","myTestState");
}大概是这么用的(直接回复时敲上去的难免有写错的地方)
super.onCreate(savedInstanceState);
// fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView tv = new TextView(this);
tv.setText("Initializing");
setContentView(tv);
downloader = new DataDownloader(this, tv);
}
TextView文本变量也要保存吗?downloader调用的对象也要保存? create以后的函数不保存有关系吗?比如说 public void initSDL()
{
mLoadLibraryStub = new LoadLibrary();
mAudioThread = new AudioThread(this);
mGLView = new DemoGLSurfaceView(this);
setContentView(mGLView);
// Receive keyboard events
mGLView.setFocusableInTouchMode(true);
mGLView.setFocusable(true);
mGLView.requestFocus();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, Globals.ApplicationName);
wakeLock.acquire();
}