2013-05-27 91 views
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 [email protected]ebd08 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 [email protected]18928 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; 
} 


} 
+0

「點擊按鈕時我出錯了。」什麼錯誤? – Rick77

+0

@ Rick77應用程序將崩潰....消息:不幸的是你的應用程序已停止。 –

+0

:D我的意思是「logcat給你什麼樣的錯誤?」。 Logcat是Android日誌記錄工具:它爲您提供了大量關於正在進行的進程的有用信息,允許您輕鬆打印調試語句(例如檢查變量)並在出現錯誤時打印回溯:http://developer.android。 com/tools/debugging/debugging-log.html – Rick77

回答

2

我覺得有些事情錯

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

我searsh利特爾位和發現可能與主題:

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

所以變通方法 「從XML取出片段並在replace()方法使用的FrameLayout部件」。

更新:Read this cerfuly並實施dinamycaly添加片段而不是靜態的xml。 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(); 

這在理論上我並沒有嘗試過自我。

相關問題