应用程序崩溃时,按钮点击片段 [英] Application Crash when button Click on fragment
问题描述
我有两个类都使用片段; FirstFragment类和注册类。使用一个片段在同一时间。
在FirstFragment有一个按钮并单击按钮,我有按钮我得到的错误,当点击转到下一个fragment.But。
下面是FirstFragment班组长的code
包com.example.bajraregistertesteclipse;进口java.util.zip.Inflater中;
进口android.app.Fragment;
进口android.app.FragmentTransaction;
进口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,loginTestConfirmPassword;
字符串的用户名,密码,confirmpassword;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根 查看查看= inflater.inflate(R.layout.firstfragment,集装箱,FALSE);
按钮btnSignup =(按钮)view.findViewById(R.id.buttonCreateAccount);
btnSignup.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
注册注册=新注册();
FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
ft.replace(R.id.listFragment,注册);
ft.addToBackStack(NULL);
ft.commit();
} }); 返回视图。
}
}
下面是activity_main XML的code:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向><片段
机器人:ID =@ + ID / listFragment
机器人:layout_width =211dp
机器人:layout_weight =1
机器人:layout_height =match_parent
机器人:layout_marginTop =机器人:ATTR / actionBarSize
类=com.example.bajraregistertesteclipse.FirstFragment
工具:布局=@布局/ firstfragment/>
感谢你在前进!
我的日志猫
日志猫
05-27 19:46:29.635:E / ActivityThread(768):在java.lang.Thread.run(Thread.java:856) 05-27 19:46:29.716:E / StrictMode(768):空 05-27 19:46:29.716:E / StrictMode(768):android.app.ServiceConnectionLeaked:服务com.android.exchange.ExchangeService渗漏ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cebd08这原是绑定这里 。05-27 19:46:29.716:E / StrictMode(768):在android.app.LoadedApk $ ServiceDispatcher<&初始化GT;(LoadedApk.java:969) 05-27 19:46:29.716:E / StrictMode(768):在android.app.ContextImpl.bindService(ContextImpl.java:1418) 05-27 19:46:29.716:E / StrictMode(768):在android.content.ContextWrapper.bindService(ContextWrapper.java:473) 05-27 19:46:29.716:E / StrictMode(768):在com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 05-27 19:46:29.716:E / StrictMode(768):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 05-27 19:46:29.716:E / StrictMode(768):在java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-27 19:46:29.716:E / StrictMode(768):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 05-27 19:47:20.355:E / ActivityThread(768):服务com.android.exchange.ExchangeService渗漏,最初这里必然ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d18928 05-27 19:47:20.355:E / ActivityThread(768):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-27 19:47:20.355:E / ActivityThread(768):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
我的注册类
包com.example.bajraregistertesteclipse;进口java.util.zip.Inflater中;进口android.app.Activity;
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类注册扩展片段{ LoginDataBaseAdapter loginDataBaseAdapter;
的EditText loginTestUser,loginTestPassword,loginTestConfirmPassword;
字符串的用户名,密码,confirmpassword; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
最后查看视图= inflater.inflate(R.layout.signup,集装箱,FALSE); loginDataBaseAdapter = loginDataBaseAdapter.open(); 最终的EditText loginTestUser =(EditText上)view.findViewById(R.id.editTextUserName);
最终的EditText loginTestPassword =(EditText上)view.findViewById(R.id.editTextPassword);
最终的EditText loginTestConfirmPassword =(EditText上)view.findViewById(R.id.editTextConfirmPassword);
按钮btnSignup =(按钮)view.findViewById(R.id.buttonCreateAccount);
btnSignup.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){ // loginTestUser =(EditText上)view.findViewById(R.id.editTextUserName);
的userName = loginTestUser.getText()的toString()。 // loginTestPassword =(EditText上)view.findViewById(R.id.editTextPassword);
。密码= loginTestPassword.getText()的toString(); // loginTestConfirmPassword =(EditText上)view.findViewById(R.id.editTextConfirmPassword);
confirmpassword = loginTestConfirmPassword.getText()的toString()。 //字符串username = loginTestUser.getText()的toString()。
//字符串密码= loginTestPassword.getText()的toString()。
//字符串confirmpassword = loginTestConfirmPassword.getText()的toString()。 //检查是否任何字段都是空置 如果(userName.equals()|| password.equals()|| confirmpassword.equals()){ Toast.makeText(getActivity(),现场Vaccant,Toast.LENGTH_LONG).show();
返回;
}
//检查两个密码匹配
如果(!password.equals(confirmpassword)){
Toast.makeText(getActivity(),密码不匹配,Toast.LENGTH_LONG).show();
返回;
}其他{
//将数据保存到数据库
loginDataBaseAdapter.insertEntry(用户名,密码);
Toast.makeText(getActivity(),账户成功塑造了,Toast.LENGTH_LONG).show(); }
}
});
返回视图。
}
}
我觉得有些东西错
ft.replace(R.id.listFragment,注册);
我searsh痘痘一下,发现可能与主题:
所以,解决方法是从XML中删除片段,并在replace()方法使用的FrameLayout的组成部分。
更新: Read这cerfuly 和实施dinamycaly添加片段,而不是静态的XML格式。谷歌提供 示例 如何到dynamicaly添加它。
改变你的XML的FrameLayout:
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fragment_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
在OnCreate()中把你的第一个片段(你需要将其移动到某个类)
//片段添加到fragment_container的FrameLayout
getSupportFragmentManager()调用BeginTransaction()
。新增(R.id.fragment_container,firstFragment).commit();
的onClick方法:
注册注册=新注册();
//片段添加到fragment_container的FrameLayout
getSupportFragmentManager()调用BeginTransaction()
。新增(R.id.fragment_container,注册).commit();
这是在理论上我不要被纽约自我尝试过。
I have two class both using fragment; FirstFragment class and SignUp class. Using one fragment at a time. In the FirstFragment there is a button and on clicking the button i have to go to next fragment.But when clicking on the button i got error.
Here is the code of FirstFragment classs
package com.example.bajraregistertesteclipse;
import java.util.zip.Inflater;
import android.app.Fragment;
import android.app.FragmentTransaction;
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,loginTestConfirmPassword;
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);
Button btnSignup = (Button) view.findViewById(R.id.buttonCreateAccount);
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SignUp signup=new SignUp();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.listFragment, signup);
ft.addToBackStack(null);
ft.commit();
}
});
return view;
}
}
Here is the code of activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/listFragment"
android:layout_width="211dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.bajraregistertesteclipse.FirstFragment"
tools:layout="@layout/firstfragment"/>
Thank you in advance!!!
My Log Cat
log cat
05-27 19:46:29.635: E/ActivityThread(768): at java.lang.Thread.run(Thread.java:856)
05-27 19:46:29.716: E/StrictMode(768): null
05-27 19:46:29.716: E/StrictMode(768): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cebd08 that was originally bound here
05-27 19:46:29.716: E/StrictMode(768): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-27 19:46:29.716: E/StrictMode(768): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-27 19:46:29.716: E/StrictMode(768): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-27 19:46:29.716: E/StrictMode(768): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-27 19:46:29.716: E/StrictMode(768): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-27 19:46:29.716: E/StrictMode(768): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-27 19:46:29.716: E/StrictMode(768): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-27 19:47:20.355: E/ActivityThread(768): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d18928 that was originally bound here
05-27 19:47:20.355: E/ActivityThread(768): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-27 19:47:20.355: E/ActivityThread(768): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
My Signup Class
package com.example.bajraregistertesteclipse;
import java.util.zip.Inflater;
import android.app.Activity;
import android.app.Fragment;
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 SignUp extends Fragment{
LoginDataBaseAdapter loginDataBaseAdapter;
EditText loginTestUser,loginTestPassword,loginTestConfirmPassword;
String userName,password,confirmpassword;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
final View view = inflater.inflate(R.layout.signup, container, false);
loginDataBaseAdapter= loginDataBaseAdapter.open();
final EditText loginTestUser = (EditText) view.findViewById(R.id.editTextUserName);
final EditText loginTestPassword = (EditText) view.findViewById(R.id.editTextPassword);
final EditText loginTestConfirmPassword = (EditText) view.findViewById(R.id.editTextConfirmPassword);
Button btnSignup = (Button) view.findViewById(R.id.buttonCreateAccount);
btnSignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//loginTestUser = (EditText) view.findViewById(R.id.editTextUserName);
userName=loginTestUser.getText().toString();
// loginTestPassword = (EditText) view.findViewById(R.id.editTextPassword);
password=loginTestPassword.getText().toString();
// loginTestConfirmPassword = (EditText) view.findViewById(R.id.editTextConfirmPassword);
confirmpassword=loginTestConfirmPassword.getText().toString();
// String userName = loginTestUser.getText().toString();
// String password = loginTestPassword.getText().toString();
// String confirmpassword = loginTestConfirmPassword.getText().toString();
//check if any of the fields are vacant
if (userName.equals("") || password.equals("") || confirmpassword.equals("")) {
Toast.makeText(getActivity(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if (!password.equals(confirmpassword)) {
Toast.makeText(getActivity(), "Password does not match", Toast.LENGTH_LONG).show();
return;
} else {
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password);
Toast.makeText(getActivity(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
I think some thing wrong with
ft.replace(R.id.listFragment, signup);
I searsh a litle bit and find may be related topics:
So workaround is "remove the fragment from the xml and use FrameLayout component in the replace() method."
UPDATE: Read this cerfuly and implement dinamycaly add Fragment instead of static in xml. Google provide example how to add it dynamicaly.
change your xml to FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
in onCreate() place your first fragment (you need to move it to some class)
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
onClick method:
SignUp signup=new SignUp();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, signup).commit();
It's in theory I don't tried it by ny self.
这篇关于应用程序崩溃时,按钮点击片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!