在我的应用程序中,我必须从活动中调用一个片段。所以我正在使用Frgament漫画。当我运行该代码时,它会引发上述异常。
这是我的主要活动
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>
这是片段listview 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 :(得分:0)
您的主要活动xml中没有容器(R.id.container
),您应该在其中显示您的片段。
喜欢
<framelayout android:id="@+id/container"/> // this snippet is just for idea.