2014-06-06 4 views
0

저는 Fragment로 확장 된 활동이 있으며 TabListner를 구현합니다. 해당 액티비티에서 TabHost (조각 TabHost 아님)를 구현할 수 있습니까? 왜냐하면 내가 시도했을 때 findViewById에서 탭 호스트를 찾을 수 없기 때문입니다. TabActivity이 프로그램에서 tabhost 방법을 얻을로TabListner를 구현하는 프래그먼트로 확장 된 활동에서 탭 호스트를 구현할 수 있습니까?

UserProfile.java

public class UserProfile extends Fragment implements TabListener{ 


    private static final int RESULT_OK = -1; 
    private static int RESULT_LOAD_IMAGE = 1; 
    Map<String, AttributeValue> map = new HashMap<String, AttributeValue>(); 
    TabHost mTabHost; 

    ImageView profilepic; 
    String userId = new PassValues().getUserId(); 
    ArrayList<UserDetailsBean> ar = new ArrayList<UserDetailsBean>(); 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.activity_user_profile, 
       container, false); 
     TextView name = (TextView) rootView.findViewById(R.id.name); 
     //profilepic = (ImageView) rootView.findViewById(R.id.profile_pic); 


     /*mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabhost); 
     mTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.tabcontent1); 

     mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), 
      UserDetailsActivity.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), 
      UserJoinedGroupsActivity.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"), 
      UserPostedDiscussionActivity.class, null);*/ 






     AsyncTask<String, String, ArrayList<UserDetailsBean>> async = new GetUserDetailsDataSource() 
       .execute(userId); 
     try { 
      ar = async.get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     for (int i = 0; i < ar.size(); i++) 
      name.setText(ar.get(i).getFirstname() + " " 
        + ar.get(i).getLastname()); 

     profilepic.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent i = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 

      } 
     }); 
     return rootView; 

    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK 
       && null != data) { 
      Uri selectedImage = data.getData(); 
      System.out.println(selectedImage); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getActivity().getContentResolver().query(
        selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 
      System.out 
        .println((new PassValues().getUserId() + new LoginSharePreferences() 
          .getEmailId(getActivity()))); 
      System.out.println(ar.get(0).getProfilePicPath()); 
      AsyncTask<String, Void, Map<String, AttributeValue>> async = new InterestGroupsListDataSource(
        userId, picturePath, 
        new LoginSharePreferences().getEmailId(getActivity()), ar 
          .get(0).getProfilePicPath(), "profile").execute(); 
      try { 
       map = async.get(); 
       System.out.println(map.get("Profile Image Path").getS()); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      // profilepic.setImageBitmap(BitmapFactory.decodeFile(map.get("Profile Image Path").getS())); 
      profilepic.setImageBitmap(BitmapFactory.decodeFile(ar.get(0) 
        .getProfilePicPath())); 

     } 

    } 

    @Override 
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 
} 

activity_user_profile.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fadingEdgeLength="@dimen/activity_horizontal_margin" 
    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=".UserProfile" > 

    <ImageView 
     android:id="@+id/profile_pic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="43dp" 
     android:minWidth="100dp" 
     android:src="@drawable/button_shape_google" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/profile_pic" 
     android:layout_marginTop="23dp" 
     android:layout_toRightOf="@+id/profile_pic" 
     android:text="Anubhav Bhardwaj" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_marginTop="180dp" 
      android:layout_height="0dip" 
      android:layout_weight="1"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="-4dp" 
       android:layout_weight="0" > 
      </TabWidget> 

     </FrameLayout> 

    </LinearLayout> 

    </TabHost> 

</RelativeLayout> 

답변

0

당신은 당신의 활동을 확장 할 수 있습니다. 액티비티가 FragmentActivity로 확장되면 리스너 클래스를 구현할 수 있지만 해당 액티비티에서 Tabhost를 가져올 수 있습니다.

그냥 난 그냥 코드를 게시 한

Public class *youactivity* extends TabActivity implements TabListener{ 
       *Your code* 
} 


<TabHost 
android:id="@+id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
</TabHost> 
+0

로 활동을 변경합니다. 당신은 그것을 볼 수 있니? ' 당신이 할 수있는 – user3713665

+0

는'<: ID = "@ + ID/tabhost"** 안드로이드 : layout_width = "fill_parent" 안드로이드 = "fill_parent"layout_height TabHost ** 안드로이드>와 같은 tabhost의 ID를 교체 findviewbyid에서 tabhost를 얻으십시오 ...! –

+0

이 작동하지 않습니다. ">"또는 "/>"속성을 사용하여 오류 시작 또는 끝내기 – user3713665