本文介绍了方向改变应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的片段,当我更改方向的应用程序崩溃。
这里是日志猫
五月五日至29日:56:52.158:E / AndroidRuntime(1428):了java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException:二进制XML文件行#16:错误充气类片段
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.app.ActivityThread.access $ 700(ActivityThread.java:141)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1240)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.os.Handler.dispatchMessage(Handler.java:99)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.os.Looper.loop(Looper.java:137)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在android.app.ActivityThread.main(ActivityThread.java:5039)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在java.lang.reflect.Method.invokeNative(本机方法)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在java.lang.reflect.Method.invoke(Method.java:511)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
6月5日至29日:17:02.864:E / AndroidRuntime(1554):了java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException:二进制XML文件行#16:错误充气类片段
6月5日至29日:17:02.864:E / AndroidRuntime(1554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
5月5日至29日:56:52.158:E / AndroidRuntime(1428):java.lang.IllegalStateException:产生的原因片段com.example.bajraregistertesteclipse.SecondFragment没有创建视图。
下面是我First_Test_Fragment类
包com.example.bajraregistertesteclipse;进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口android.content.res.Configuration;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.Toast;公共类First_Test_Fragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
查看查看= inflater.inflate(R.layout.first_test_fragment,集装箱,FALSE);
按钮btnLogin =(按钮)view.findViewById(R.id.btnLogin); btnLogin.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
FirstFragment firstFragment =新FirstFragment();
FragmentTransaction fragmentTransaction = getFragmentManager()调用BeginTransaction()。
fragmentTransaction.replace(R.id.detailFragment,firstFragment);
fragmentTransaction.addToBackStack(NULL);
fragmentTransaction.commit(); } });
按钮btnSignUp =(按钮)view.findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
注册注册=新注册();
FragmentTransaction fragmentTransaction = getFragmentManager()调用BeginTransaction()。
fragmentTransaction.replace(R.id.detailFragment,注册);
fragmentTransaction.addToBackStack(NULL);
fragmentTransaction.commit(); } });
返回视图。
}}}
下面是我的FirstFragment类
包com.example.bajraregistertesteclipse;进口android.app.Fragment;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类FirstFragment扩展片段{
LoginDataBaseAdapter loginDataBaseAdapter;
的EditText loginTestUser,loginTestPassword;
字符串的用户名,密码,confirmpassword;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根 查看查看= inflater.inflate(R.layout.firstfragment,集装箱,FALSE);
loginTestUser =(EditText上)view.findViewById(R.id.editTextUserNameToLogin);
loginTestPassword =(EditText上)view.findViewById(R.id.editTextPasswordToLogin);
按钮btnLogin =(按钮)view.findViewById(R.id.buttonSignIn); btnLogin.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
// Toast.makeText(getActivity(),BttonLogin pressed,Toast.LENGTH_SHORT).show(); //获取用户名和密码
的userName = loginTestUser.getText()的toString()。
。密码= loginTestPassword.getText()的toString(); //为获取相应的用户名的密码形式数据库
字符串storedPassword = loginDataBaseAdapter.getSinlgeEntry(用户名); //检查存储的密码与密码匹配由用户输入
如果(password.equals(storedPassword))
{
Toast.makeText(getActivity(),恭喜:登录全成,Toast.LENGTH_LONG).show(); //意向意图=新意图(getActivity(),Admin_Page.class);
// getActivity()startActivity(意向)。 意图打开=新意图(com.example.bajraregistertesteclipse.ADMIN_PAGE); 。getActivity()startActivity(开);
// startActivity(开); }
其他
{
Toast.makeText(getActivity(),用户名或密码不匹配,Toast.LENGTH_LONG).show();
} } }); 返回视图。
}
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
loginDataBaseAdapter =新LoginDataBaseAdapter(getActivity());
loginDataBaseAdapter = loginDataBaseAdapter.open();
}
}
我的Mainfest.xml是:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.bajraregistertesteclipse
安卓版code =1
机器人:=的versionName1.0><用途-SDK
安卓的minSdkVersion =13
机器人:targetSdkVersion =16/><应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme> <活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=com.example.bajraregistertesteclipse.MAINACTIVITY/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=注册>< /活性GT;
<活动机器人:名字=。Admin_Page> < /活性GT;< /用途>
现在我的问题是应用程序崩溃时的方向变化,当点击该按钮新活动Admin_Page应initiatied,但应用程序崩溃。
感谢你在前进!
解决方案
的android:configChanges =keyboardHidden |方向
在您的清单文件在你的活动添加这一点。
I am using fragments and when i change the orientation the application crash.Here is the log cat:
05-29 05:56:52.158: E/AndroidRuntime(1428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.app.ActivityThread.access$700(ActivityThread.java:141)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.os.Looper.loop(Looper.java:137)
05-29 05:56:52.158: E/AndroidRuntime(1428): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-29 05:56:52.158: E/AndroidRuntime(1428): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 05:56:52.158: E/AndroidRuntime(1428): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 05:56:52.158: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-29 05:56:52.158: E/AndroidRuntime(1428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-29 06:17:02.864: E/AndroidRuntime(1554): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bajraregistertesteclipse/com.example.bajraregistertesteclipse.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
05-29 06:17:02.864: E/AndroidRuntime(1554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-29 05:56:52.158: E/AndroidRuntime(1428): Caused by: java.lang.IllegalStateException: Fragment com.example.bajraregistertesteclipse.SecondFragment did not create a view.
Here is my First_Test_Fragment class
package com.example.bajraregistertesteclipse;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class First_Test_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.first_test_fragment,container,false);
Button btnLogin=(Button)view.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FirstFragment firstFragment=new FirstFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.detailFragment, firstFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
Button btnSignUp=(Button)view.findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SignUp signup=new SignUp();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.detailFragment, signup);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
}
Here is my FirstFragment Class
package com.example.bajraregistertesteclipse;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FirstFragment extends Fragment {
LoginDataBaseAdapter loginDataBaseAdapter;
EditText loginTestUser,loginTestPassword;
String userName,password,confirmpassword;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.firstfragment, container, false);
loginTestUser=(EditText)view.findViewById(R.id.editTextUserNameToLogin);
loginTestPassword= (EditText)view.findViewById(R.id.editTextPasswordToLogin);
Button btnLogin = (Button) view.findViewById(R.id.buttonSignIn);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Toast.makeText(getActivity(),"BttonLoginPressed",Toast.LENGTH_SHORT).show();
// get The User name and Password
userName=loginTestUser.getText().toString();
password=loginTestPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(getActivity(), "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
// Intent intent = new Intent(getActivity(), Admin_Page.class);
// getActivity().startActivity(intent);
Intent open=new Intent("com.example.bajraregistertesteclipse.ADMIN_PAGE");
getActivity().startActivity(open);
// startActivity(open);
}
else
{
Toast.makeText(getActivity(), "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loginDataBaseAdapter=new LoginDataBaseAdapter(getActivity());
loginDataBaseAdapter=loginDataBaseAdapter.open();
}
}
My Mainfest.xml is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bajraregistertesteclipse"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.bajraregistertesteclipse.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUp"></activity>
<activity android:name=".Admin_Page">
</activity>
</application>
Now my problem is Application crash when orientation change and When click to the button new activity Admin_Page should be initiatied but application crash.
Thank YOu in Advance!!!
解决方案
android:configChanges="keyboardHidden|orientation"
add this in your manifest file in your activity.
这篇关于方向改变应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!