0

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


}
Bhim Prasad Ale
  • 523
  • 1
  • 8
  • 31
  • "when clicking on the button i got error.". What error? – Rick77 May 27 '13 at 19:25
  • @Rick77 application will crash....Message: Unfortunately your application has stopped. – Bhim Prasad Ale May 27 '13 at 19:28
  • :D I meant "what kind of error does logcat give to you?". Logcat is the Android logging facility: it gives you a lot of useful information about the ongoing processes, allows you to easily print debugging statements (e.g. to inspect variables) and prints a traceback when things go awry: http://developer.android.com/tools/debugging/debugging-log.html – Rick77 May 27 '13 at 19:29
  • can you show code for SignUp signup=new SignUp(); Because your problem related to some leak: android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cebd08 that was originally bound here – Borys May 27 '13 at 20:19
  • @Borys i have updated my signup class. – Bhim Prasad Ale May 27 '13 at 20:23
  • StrictMode, ExchangeService, ServiceConnection - is it your classes? Because your errors somehow related to those classes. There is one black box point - loginDataBaseAdapter= loginDataBaseAdapter.open(); – Borys May 27 '13 at 20:35
  • @Borys sorry for late rply....no strictmode exchange service is not a class – Bhim Prasad Ale May 28 '13 at 05:27
  • 05-28 05:48:56.512: E/AndroidRuntime(1543): java.lang.RuntimeException: Unable to create service com.android.systemui.SystemUIService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@4103bcb0 -- another window of this type already exists I got these error too – Bhim Prasad Ale May 28 '13 at 06:05
  • Can you update Logcut with error around WindowManager$BadTokenException. Seems it's your problem. – Borys May 28 '13 at 07:12
  • @Borys how can we replace the new UI fragment replacing existing UI in fragments. – Bhim Prasad Ale May 28 '13 at 08:02
  • I update my answer, look here and try workaround from that links. – Borys May 28 '13 at 10:18
  • @Borys thank you for the answer.it worked. now the problem is the Handling Run-Time Configuration Changes in fragments. When i rotate the screen. As i did research i came to know that i have to use like " setRetainInstance(true); but i dont know how to use it..Do you have any idea on it ??? – Bhim Prasad Ale May 28 '13 at 11:38

1 Answers1

2

I think some thing wrong with

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

I searsh a litle bit and find may be related topics:

  1. FragmentTransaction.replace() not working
  2. Fragment duplication on Fragment Transaction

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.

Community
  • 1
  • 1
Borys
  • 1,793
  • 2
  • 17
  • 32