按钮时应用程序崩溃单击片段

时间:2013-05-27 19:22:14

标签: android fragment

我有两个类都使用片段; FirstFragment类和SignUp类。一次使用一个片段。 在FirstFragment中有一个按钮,点击按钮我必须转到下一个片段。但是当点击按钮时我得到了错误。

这是FirstFragment类的代码

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;
}


}

以下是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"/>

提前谢谢!!!

我的日志猫

   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)

我的注册课程

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;
}


}

1 个答案:

答案 0 :(得分:2)

我觉得

有些不对劲
 ft.replace(R.id.listFragment, signup);

我搜索了一下,发现可能是相关主题:

  1. FragmentTransaction.replace() not working
  2. Fragment duplication on Fragment Transaction
  3. 所以解决方法是“从xml中删除片段并在replace()方法中使用FrameLayout组件。”

    更新: Read this cerfuly并在xml中实现dinamycaly添加Fragment而不是static。 Google提供example如何动态添加动态。

    将您的xml更改为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" />
    
    在onCreate()中放置你的第一个片段(你需要把它移到某个类)

      // Add the fragment to the 'fragment_container' FrameLayout
      getSupportFragmentManager().beginTransaction()
              .add(R.id.fragment_container, firstFragment).commit();
    

    onClick方法:

        SignUp signup=new SignUp();
        // Add the fragment to the 'fragment_container' FrameLayout
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, signup).commit();
    

    理论上我并没有尝试过自己。