2016-02-15 40 views
1

在我的應用程序中,我必須從活動調用一個片段。所以我使用Frgament漫畫家。當我運行該代碼時,它會引發上述異常。在調用來自活動的片段時,沒有爲ID 0x7f0c0078找到視圖

這是我的主要活動

public class UserDashBoardActivity extends DrawerActivity { 


     private Context context; 
     private ImageButton searchBtn; 
     private ImageButton favBtn; 
     private ImageButton profileBtn; 
     private ImageButton reminderBtn; 
     private ImageButton logoutBtn; 
     private ImageButton notificationBtn; 
     private ImageView seatchIcon; 
     private DrawerLayout mDrawerLayout; 
     private ListView mDrawerList; 
     private ActionBarDrawerToggle mDrawerToggle; 
     // slide menu items 
     private String[] navMenuTitles; 
     private TypedArray navMenuIcons; 

     @Override 
     protected void onStart() { 
      super.onStart(); 
      AppActivityStatus.setActivityStarted(); 
      AppActivityStatus.setActivityContext(context); 
     } 

     @Override 
     protected void onPause() { 
      super.onPause(); 
      AppActivityStatus.setActivityStoped(); 

     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 
      AppActivityStatus.setActivityStarted(); 
     } 

     @Override 
     protected void onStop() { 
      super.onStop(); 
      AppActivityStatus.setActivityStoped(); 
     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_user_dash_boad, menu); 
      return true; 
     } 

     // delete the selected event from event list added here 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      switch (item.getItemId()) { 
       case R.id.action_notify: 
        return true; 

       case R.id.action_favourite: 
        return true; 
       case R.id.action_navigation: 

      } 

      return super.onOptionsItemSelected(item); 
     } 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.user_dash_board); 
      context = getApplicationContext(); 
      searchBtn = (ImageButton) findViewById(R.id.search_btn); 
      favBtn = (ImageButton) findViewById(R.id.fav_btn); 
      profileBtn = (ImageButton) findViewById(R.id.profile_btn); 
      reminderBtn = (ImageButton) findViewById(R.id.reminder_btn); 
      notificationBtn = (ImageButton) findViewById(R.id.notification_btn); 
      logoutBtn = (ImageButton) findViewById((R.id.logout_btn)); 
      final EditText Search = (EditText) findViewById(R.id.emailAddress); 
      searchBtn.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        Intent regAct = new Intent(getApplicationContext(), SearchActivity.class); 
        // Clears History of Activity 
        regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(regAct); 
       } 
      }); 

      favBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View arg0) { 
//here i am calling the fragment 
        MyFavouritesFragment fragment = new MyFavouritesFragment(); 


        if (fragment != null) { 
         android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
         fragmentManager.beginTransaction() 
           .replace(R.id.container, fragment).commit(); 
        } 
       } 
      }); 

      profileBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View arg0) { 
        Intent tabAct = new Intent(getApplicationContext(), AboutCollegeFragment.class); 
        tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(tabAct); 

       } 
      }); 

     } 
    } 

這是我的片段被稱爲

public class MyFavouritesFragment extends Fragment { 
    private FavouriteDelegates favouriteDelegates = new FavouriteDelegates(); 
    private Gson gson = new Gson(); 
    private Context context; 
    private List<CollegeMO> collegeMOs = new ArrayList<>(); 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final View view = inflater.inflate(R.layout.my_favourites_list_view, container, false); 
return view; 

    } 

    private class FavouriteCollege extends BaseAdapter { 
      LayoutInflater mInflater; 
      TextView collegeText; 


      FavouriteCollege() { 
       mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 

      @Override 
      public int getCount() { 
       return collegeMOs.size(); 
      } 

      @Override 
      public Object getItem(int position) { 
       return position; 
      } 

      @Override 
      public long getItemId(int position) { 
       return 0; 
      } 

      // show list values name and mobile number in contact page 
      @Override 
      public View getView(final int position, View convertView, ViewGroup parent) { 

       if (convertView == null) 
        convertView = mInflater.inflate(R.layout.my_favourites, null); 
       collegeText = (TextView) convertView.findViewById(R.id.clg_details); 
       collegeText.setText(collegeMOs.get(position).getCollegeName()); 
       return convertView; 
      } 

     } 
    } 

這是我的主要活動XML

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



    <ImageButton 
     android:id="@+id/search_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="150dp" 
     android:layout_marginTop="70dp" 
     android:background="@drawable/search_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/searchCollege" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="80dp" 
     android:layout_marginRight="100dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/search_college" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 

    <ImageButton 
     android:id="@+id/fav_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="50dp" 
     android:layout_marginLeft="150dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="-167dp" 
     android:background="@drawable/fav_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/myFavourites" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="370dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="-30dp" 
     android:text="@string/my_favourites" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 

    <ImageButton 
     android:id="@+id/profile_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_marginLeft="90dp" 
     android:layout_marginRight="150dp" 
     android:layout_marginTop="30dp" 
     android:background="@drawable/profile_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/myProfile" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="105dp" 
     android:layout_marginRight="100dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/my_profile" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 

    <ImageButton 
     android:id="@+id/notification_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="150dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="-165dp" 
     android:background="@drawable/notification_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/notification" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="390dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/notification" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 

    <ImageButton 
     android:id="@+id/reminder_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="55dp" 
     android:layout_marginRight="200dp" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/reminder_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/reminder" 
     android:layout_width="wrap_content" 
     android:layout_height="80dp" 
     android:layout_marginLeft="110dp" 
     android:layout_marginRight="100dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/reminder" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 

    <ImageButton 
     android:id="@+id/logout_btn" 
     android:layout_width="115dp" 
     android:layout_height="120dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="150dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="-220dp" 
     android:background="@drawable/logout_blue" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/logout" 
     android:layout_width="wrap_content" 
     android:layout_height="80dp" 
     android:layout_marginBottom="50dp" 
     android:layout_marginLeft="410dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/logout" 
     android:textColor="@color/green" 
     android:textSize="20sp" /> 
    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="1000dp" 
     android:layout_height="1000dp" 
     android:layout_gravity="center" 
     > 
    </FrameLayout> 
</LinearLayout> 

這是片段的Lis​​tView XML

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

    <ListView 
     android:id="@+id/course_detail_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:scrollbarStyle="outsideOverlay" /> 

</LinearLayout> 

這是myfavourite的ListView XML的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/appblue"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:layout_marginBottom="40dp" 
     android:layout_marginLeft="40dp" 
     android:layout_marginRight="40dp" 
     android:layout_marginTop="40dp" 
     android:background="@color/white"> 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
     <ImageView 
      android:id="@+id/clgImage" 
      android:src = "@drawable/ic_launcher" 
      android:layout_weight="1" android:background="@color/white" 
      android:padding="20dip" android:gravity="center"/> 
     <TextView 
      android:id="@+id/clg_details" android:text="Row 2 column 2" 
      android:layout_weight="1" android:background="@color/white" 
      android:textColor="#000000" 
      android:padding="20dip" android:gravity="center"/> 
      <ImageView 
       android:id="@+id/downloadImage" 
       android:src = "@drawable/ic_launcher" 
       android:layout_weight="1" android:background="@color/white" 
       android:padding="20dip" android:gravity="center"/> 
     </TableRow> 

    </LinearLayout> 

    </LinearLayout> 
+0

粘貼您的片段類。它的失蹤。 – Nithinjith

+0

現在我添加的代碼,供大家參考 –

回答

0

你沒有在你的主要活動的XML容器(R.id.container),您應該顯示您的片段到的項目。

<framelayout android:id="@+id/container"/> // this snippet is just for idea. 
+0

,如果我再補充一點代碼佈局打開,但它是小,它的主要活動下運行結束 –

+0

你可以設置你的容器高度寬度能正確顯示和當然會下的主要活動。片段應該有父項活動。他們不是那樣的獨立。 – KDeogharkar

+0

好的,謝謝,我會嘗試它,如果它的作品,我會告訴你 –

相關問題