0

我有两个班级都使用片段;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;
}


}
4

1 回答 1

2

我觉得有些不对劲

 ft.replace(R.id.listFragment, signup);

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

  1. FragmentTransaction.replace() 不工作
  2. 分片交易中的分片重复

所以解决方法是“从 xml 中删除片段并在 replace() 方法中使用 FrameLayout 组件”。

更新: 仔细阅读并在 xml 中实现 dinamycaly 添加片段而不是静态。Google 提供了如何动态添加它的示例。

将您的 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();

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

于 2013-05-27T19:43:50.077 回答