2013-04-16 5 views
0

В настоящее время я изучаю программирование фрагментов Android в течение нескольких часов, однако мои фрагменты не отображаются. У меня нет ошибок. Отладка показывает, что onCreateView() класса фрагмента никогда не вызывается.Фрагмент Android, не показывающий

Это мой основной макет activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<LinearLayout 
    android:id="@+id/FragCont" 
    android:layout_width="100dp" 
    android:layout_height="200dp" 
    android:layout_alignParentRight="true" 
    android:layout_toRightOf="@+id/textView1" 
    android:orientation="vertical" > 

</LinearLayout> 

Это мой Основная деятельность:

package k.myApp; 

import com.actionbarsherlock.app.SherlockActivity; 
import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.app.SherlockFragmentActivity; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.FragmentTransaction; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends FragmentActivity { 

SherlockFragment Fragment; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    CentralData.getDataSource().open(); 
    CentralData.getDataSource().close(); 


    FragmentShowNotes fsn = new FragmentShowNotes(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.FragCont, fsn); 
    transaction.commit(); 
} 
} 

Это мой класс фрагмент, FragmentShowNotes.java: пакет к .myapp;

import org.holoeverywhere.LayoutInflater; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 

import com.actionbarsherlock.app.SherlockFragment; 

public class FragmentShowNotes extends Fragment{ 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
     return inflater.inflate(R.layout.frag_show_notes, container, false); 
} 
} 

И последнее, но не менее мой макет фрагмента, frag_show_notes.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Dies ist das ShowNotesFragment" /> 

</LinearLayout> 

Кто-нибудь знает, что происходит не так? Спасибо!

+0

Любые исключения ...? – Triode

+0

нет, никаких ошибок или исключений – user1956123

ответ

1

Попробуйте

getSupportFragmentManager (you use SherlockActionBar) 
0

попробовать, как этот

getSupportFragmentManager().beginTransaction().add(android.R.id.content, fsn).commit(); 
0

другим способом, чтобы заменить вас LinearLayout на com.actionbarsherlock.app.SherlockFragment.FragmentShowNotes

+0

Спасибо вам за помощь! Теперь я запрограммировал новый проект только с фрагментами и без Holoeverywhere, и он сработал. Ошибка в моем проекте была в классе фрагментов (FragmentShowNotes.java): Я импортировал org.holoeverywhere.LayoutInflater, изменив его на android.view.LayoutInflater решил. Надеюсь, это не повлияет на стиль Holo на старых устройствах, но я не верю :) – user1956123

0

Спасибо всем за вашу помощь! Теперь я запрограммировал новый проект только с фрагментами и без Holoeverywhere, и он сработал.

Ошибка в моем проекте была в классе фрагмента: Я импортировал org.holoeverywhere.LayoutInflater, изменив его на android.view.LayoutInflater решил. Надеюсь, это не повлияет на стиль Holo на старых устройствах, но я не верю :)

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.ripa.chatapplication.FriendsFragment"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/friends_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </android.support.v7.widget.RecyclerView> 




</RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"> 

    <de.hdodenhof.circleimageview.CircleImageView 
     android:layout_width="90sp" 
     android:layout_height="90sp" 
     android:src="@drawable/index" 
     android:id="@+id/user_single_image" /> 

    <TextView 
     android:id="@+id/user_single_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginEnd="50dp" 
     android:layout_marginTop="11dp" 
     android:text="Display Name" 
     android:textColor="#000000" 
     android:textSize="25sp" /> 

    <TextView 
     android:id="@+id/user_single_status" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/user_single_image" 
     android:layout_alignEnd="@+id/user_single_name" 
     android:layout_marginBottom="12dp" 
     android:text="@string/user_default_status" 
     android:textColor="#000000" 
     android:textSize="20sp" /> 


</RelativeLayout> 

package com.example.ripa.chatapplication; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.firebase.ui.database.FirebaseRecyclerAdapter; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 

import java.util.jar.Attributes; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class FriendsFragment extends Fragment { 
    private RecyclerView mFriendsList; 
    private DatabaseReference mUsersDatabase; 
    private DatabaseReference mFriendsDatabase; 
    private String mCurrent_user_id; 
    private FirebaseAuth mAuth; 
    private View mMainView; 


    public FriendsFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     mMainView = inflater.inflate(R.layout.fragment_friends, container, false); 


     mFriendsList =(RecyclerView)mMainView.findViewById(R.id.friends_list); 
     mAuth =FirebaseAuth.getInstance(); 
     mCurrent_user_id = mAuth.getCurrentUser().getUid(); 
     mFriendsDatabase = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id); 
     mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users"); 

     mFriendsList.setHasFixedSize(true); 
     mFriendsList.setLayoutManager(new LinearLayoutManager(getContext())); 

     return mMainView; 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     FirebaseRecyclerAdapter<Friends, FriendsViewHolder> FriendsRecylerViewAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(

       Friends.class, 
       R.id.users_single_layout, 
       FriendsViewHolder.class, 
       mFriendsDatabase 


     ) { 
      @Override 
      protected void populateViewHolder(final FriendsViewHolder friendsViewHolder, Friends friends, int i) { 


       friendsViewHolder.setDate(friends.getDate()); 

       String list_user_id =getRef(i).getKey(); 
       mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 

         String userName = dataSnapshot.child("name").getValue().toString(); 
         String userThumb =dataSnapshot.child("thumb_image").getValue().toString(); 

         friendsViewHolder.setName(userName); 

        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 

        } 
       }); 

      } 
     }; 
     mFriendsList.setAdapter(FriendsRecylerViewAdapter); 

    } 

    public static class FriendsViewHolder extends RecyclerView.ViewHolder{ 
     View mView; 

     public FriendsViewHolder(View itemView) { 
      super(itemView); 
      mView = itemView; 

     } 

     public void setDate(String date){ 

      TextView userNameView =(TextView) mView.findViewById(R.id.user_single_status); 
      userNameView.setText(date); 



     } 




     public void setName(String name){ 


      TextView userNameView =(TextView) mView.findViewById(R.id.user_single_name); 
      userNameView.setText(name); 





     } 
    } 
} 
+1

Объяснение было бы полезно. – jHilscher

Смежные вопросы