我是android的新手,目前正在开发一个项目,我必须从活动类中打开一个Fragment。 我使用appCompat活动作为AlertDialog,点击确定按钮我想在Framelayout中打开一个片段。 但不幸的是,我收到的错误是找不到用于片段的id的视图:
AlertDriver活动:
public class AlertDriver extends AppCompatActivity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog);
Log.e("EyeSens", "Inside AlertDriver - onCreate");
mp = MediaPlayer.create(this, R.raw.wakeup);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("SafeSnooze alert: ");
// set dialog message
alertDialog
.setMessage("Sleepy? Go to the nearby rest areas.")
//.setCancelable(false)
.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
stopService(getIntent());
//release mp3
mp.stop();
mp.release();
try {
Log.e("EyeSens", "Cancel pressed - Starting camera
again");
if (!isCameraRunning) {
//Initializing EyeTracking Constructor
new EyeTracking();
//noinspection MissingPermission
mCameraSource.start();
}
} catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
finish();
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
//release mp3
mp.stop();
mp.release();
// if this button is clicked, just close
// the dialog box and do nothing
Log.e("EyeSens", "Ok pressed - Starting
LocationCategoriesFragment");
try {
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
// fragmentTransaction.add(R.id.fragment_container, new
LocationCategoriesFragment(), "AlertLocation");
fragmentTransaction.replace(R.id.fragment_container, new
LocationCategoriesFragment());
fragmentTransaction.commit();
} catch (Exception e) {
Log.e("EyeSens", "Exception thrown: "+e.getMessage());
e.printStackTrace();
}
stopService(getIntent());
dialog.cancel();
finish();
}
});
LocationCategoriesFragment Fragment:
public class LocationCategoriesFragment extends Fragment {
public static String REQUEST_URL = "";
public ArrayList<String> categories = new ArrayList<String>();
public ArrayList<Category> catData = new ArrayList<>();
public ListView categoriesLV = null;
public TextView error;
public LocationCategoriesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_location_categories,
container, false);
categoriesLV = (ListView) view.findViewById(android.R.id.list);
final GlobalClass globalVariable = (GlobalClass)
getActivity().getApplicationContext();
globalVariable.setLocation(false);
//categories = fetchCategories(categoriesLV);
return view;
}
}
清单文件配置:
....
<activity
android:name=".util.AlertDriver"
android:theme="@style/Theme.AppCompat.Light.Dialog.Alert"/>
app_bar_navigation.xml Framelayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kumarhar.eyerecog.Navigation">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp" />
错误:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00e2 (com.example.kumarhar.eyerecog:id/fragment_container) for fragment LocationCategoriesFragment{2e9a6264 #0 id=0x7f0f00e2 AlertLocation}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
alertdialog.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">
</LinearLayout>
注意:我想通过setPositiveButton监听器从AlertDriver AppCompatActivity打开LocationCategoriesFragment片段。
请帮助..