问题
When I try to start a fragment from an activity class I keep getting a
No view found for id 0x7f0900ad frame_container2
error. The code then crashes after progress dialog shows up and moves to the fragment I'm trying to starting.
When it crashes it gives me an error like this:
05-23 12:02:32.271: E/FragmentManager(4295): No view found for id 0x7f0900ad (com.fitserv.androidapp:id/frame_container2) for fragment UserWorkoutPlanFragment{4165dbc8 #0 id=0x7f0900ad}
05-23 12:02:32.271: E/FragmentManager(4295): Activity state:
05-23 12:02:32.281: E/FragmentManager(4295): Local Activity 40cdf1d0 State:
05-23 12:02:32.291: E/FragmentManager(4295): mResumed=true mStopped=false mFinished=false
05-23 12:02:32.291: E/FragmentManager(4295): mLoadersStarted=true
05-23 12:02:32.291: E/FragmentManager(4295): mChangingConfigurations=false
05-23 12:02:32.301: E/FragmentManager(4295): mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h407dp 160dpi smll port finger qwerty/v/v -nav/h s.6}
05-23 12:02:32.301: E/FragmentManager(4295): Active Fragments in 40d72310:
05-23 12:02:32.331: E/FragmentManager(4295): #0: UserWorkoutPlanFragment{4165dbc8 #0 id=0x7f0900ad}
05-23 12:02:32.331: E/FragmentManager(4295): mFragmentId=#7f0900ad mContainerId=#7f0900ad mTag=null
05-23 12:02:32.341: E/FragmentManager(4295): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
05-23 12:02:32.341: E/FragmentManager(4295): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
05-23 12:02:32.351: E/FragmentManager(4295): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
05-23 12:02:32.351: E/FragmentManager(4295): mRetainInstance=false mRetaining=false mUserVisibleHint=true
05-23 12:02:32.351: E/FragmentManager(4295): mFragmentManager=FragmentManager{40d72310 in NewWorkout{40cdf1d0}}
05-23 12:02:32.361: E/FragmentManager(4295): mActivity=com.fitserv.user.profilemenu.NewWorkout@40cdf1d0
05-23 12:02:32.361: E/FragmentManager(4295): Added Fragments:
05-23 12:02:32.361: E/FragmentManager(4295): #0: UserWorkoutPlanFragment{4165dbc8 #0 id=0x7f0900ad}
05-23 12:02:32.361: E/FragmentManager(4295): Back Stack Indices:
05-23 12:02:32.381: E/FragmentManager(4295): #0: BackStackEntry{4165dd08 #0}
05-23 12:02:32.381: E/FragmentManager(4295): FragmentManager misc state:
05-23 12:02:32.401: E/FragmentManager(4295): mActivity=com.fitserv.user.profilemenu.NewWorkout@40cdf1d0
05-23 12:02:32.401: E/FragmentManager(4295): mContainer=android.app.Activity$1@416459b8
05-23 12:02:32.411: E/FragmentManager(4295): mCurState=5 mStateSaved=false mDestroyed=false
05-23 12:02:32.411: E/FragmentManager(4295): View Hierarchy:
05-23 12:02:32.421: E/FragmentManager(4295): com.android.internal.policy.impl.PhoneWindow$DecorView{40d45ed0 V.E..... R.....I. 0,0-320,432}
05-23 12:02:32.421: E/FragmentManager(4295): android.widget.LinearLayout{416ae1d8 V.E..... ......I. 0,0-320,432}
05-23 12:02:32.431: E/FragmentManager(4295): com.android.internal.widget.ActionBarContainer{40d5aba8 V.ED.... ......I. 0,25-320,73 #1020359 android:id/action_bar_container}
05-23 12:02:32.431: E/FragmentManager(4295): com.android.internal.widget.ActionBarView{40d27848 V.E..... ......I. 0,0-320,48 #102035a android:id/action_bar}
05-23 12:02:32.441: E/FragmentManager(4295): android.widget.LinearLayout{40d47ae0 V.....C. ......I. 9,0-113,48}
05-23 12:02:32.451: E/FragmentManager(4295): com.android.internal.widget.ActionBarView$HomeView{40d59f60 V.E..... ......I. 0,0-40,48}
05-23 12:02:32.451: E/FragmentManager(4295): android.widget.ImageView{40d29198 G.ED.... ......I. 0,0-0,0 #1020246 android:id/up}
05-23 12:02:32.461: E/FragmentManager(4295): android.widget.ImageView{4168f1c8 V.ED.... ......I. 4,8-36,40 #102002c android:id/home}
05-23 12:02:32.461: E/FragmentManager(4295): android.widget.LinearLayout{4169c5b0 V.E..... ......I. 40,0-104,48}
05-23 12:02:32.471: E/FragmentManager(4295): android.widget.ImageView{40d28350 G.ED.... ......I. 0,0-0,0 #1020246 android:id/up}
05-23 12:02:32.491: E/FragmentManager(4295): android.widget.LinearLayout{40d56b88 V.E..... ......I. 0,11-56,36}
05-23 12:02:32.491: E/FragmentManager(4295): android.widget.TextView{40d44580 V.ED.... ......I. 0,0-56,25 #102024d android:id/action_bar_title}
05-23 12:02:32.491: E/FragmentManager(4295): android.widget.TextView{40d474f8 G.ED.... ......I. 0,0-0,0 #102024e android:id/action_bar_subtitle}
05-23 12:02:32.501: E/FragmentManager(4295): com.android.internal.view.menu.ActionMenuView{40d24448 V.ED.... ......ID 320,0-320,48}
05-23 12:02:32.511: E/FragmentManager(4295): com.android.internal.widget.ActionBarContextView{40d57b78 G.E..... ......ID 0,0-0,0 #102035b android:id/action_context_bar}
05-23 12:02:32.521: E/FragmentManager(4295): android.widget.FrameLayout{41658cd8 V.ED.... ......I. 0,73-320,432 #1020002 android:id/content}
05-23 12:02:32.531: E/FragmentManager(4295): android.widget.ScrollView{41645e50 VFED.V.. ......I. 0,0-320,359}
05-23 12:02:32.531: E/FragmentManager(4295): android.widget.LinearLayout{40d61190 V.E..... ......I. 0,0-320,587}
05-23 12:02:32.531: E/FragmentManager(4295): android.widget.TextView{40d48360 V.ED.... ......I. 0,0-320,33}
05-23 12:02:32.551: E/FragmentManager(4295): android.widget.EditText{40d1da60 VFED..CL ......I. 5,38-315,63 #7f09008b app:id/inputWorkoutName}
05-23 12:02:32.551: E/FragmentManager(4295): android.widget.TextView{40d1e588 V.ED.... ......I. 0,68-320,101}
05-23 12:02:32.581: E/FragmentManager(4295): android.widget.EditText{40d1fb60 VFED..CL ......I. 5,106-315,131 #7f09008c app:id/inputWorkoutDate}
05-23 12:02:32.581: E/FragmentManager(4295): android.widget.TextView{40d448d8 V.ED.... ......I. 0,136-320,169}
05-23 12:02:32.591: E/FragmentManager(4295): android.widget.EditText{40d469e8 VFED..CL ......I. 5,174-315,199 #7f09008d app:id/inputExceriseName}
05-23 12:02:32.601: E/FragmentManager(4295): android.widget.TextView{40d465d8 V.ED.... ......I. 0,204-320,237}
05-23 12:02:32.601: E/FragmentManager(4295): android.widget.EditText{40d4af38 VFED..CL .F....I. 5,242-315,267 #7f09008e app:id/inputSets}
05-23 12:02:32.601: E/FragmentManager(4295): android.widget.TextView{416947d8 V.ED.... ......I. 0,272-320,305}
05-23 12:02:32.611: E/FragmentManager(4295): android.widget.EditText{40d23090 VFED..CL ......I. 5,310-315,335 #7f09008f app:id/inputKg}
05-23 12:02:32.621: E/FragmentManager(4295): android.widget.TextView{40d486b8 V.ED.... ......I. 0,340-320,373}
05-23 12:02:32.621: E/FragmentManager(4295): android.widget.EditText{4168f578 VFED..CL ......I. 5,378-315,403 #7f090090 app:id/inputReps}
05-23 12:02:32.621: E/FragmentManager(4295): android.widget.TextView{4168fd90 V.ED.... ......I. 0,408-320,441}
05-23 12:02:32.631: E/FragmentManager(4295): android.widget.EditText{416900a8 VFED..CL ......I. 5,446-315,534 #7f090091 app:id/inputNotes}
05-23 12:02:32.641: E/FragmentManager(4295): android.widget.Button{416cc998 VFED..C. ......I. 0,539-320,587 #7f090092 app:id/btnCreateWorkout}
05-23 12:02:32.660: E/FragmentManager(4295): com.android.internal.widget.ActionBarContainer{41664298 G.ED.... ......ID 0,0-0,0 #102035c android:id/split_action_bar}
05-23 12:02:32.660: D/AndroidRuntime(4295): Shutting down VM
05-23 12:02:32.660: W/dalvikvm(4295): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-23 12:02:32.691: E/AndroidRuntime(4295): FATAL EXCEPTION: main
05-23 12:02:32.691: E/AndroidRuntime(4295): java.lang.IllegalArgumentException: No view found for id 0x7f0900ad (com.fitserv.androidapp:id/frame_container2) for fragment UserWorkoutPlanFragment{4165dbc8 #0 id=0x7f0900ad}
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.BackStackRecord.run(BackStackRecord.java:682)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.os.Handler.handleCallback(Handler.java:725)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.os.Looper.loop(Looper.java:137)
05-23 12:02:32.691: E/AndroidRuntime(4295): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-23 12:02:32.691: E/AndroidRuntime(4295): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 12:02:32.691: E/AndroidRuntime(4295): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 12:02:32.691: E/AndroidRuntime(4295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-23 12:02:32.691: E/AndroidRuntime(4295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-23 12:02:32.691: E/AndroidRuntime(4295): at dalvik.system.NativeStart.main(Native Method)
This is my NewWorkout class, that performing the FragmentTransaction:
public class NewWorkout extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputWorkoutName;
EditText inputWorkoutDate;
EditText inputExceriseName;
EditText inputSets;
EditText inputKg;
EditText inputReps;
EditText inputNotes;
// url to create new product
private static String url_create_workout = "http://ec2-54-77-51-119.eu-west-1.compute.amazonaws.com/android_connect/create_workout.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_addworkout);
// Edit Text
inputWorkoutName = (EditText) findViewById(R.id.inputWorkoutName);
inputWorkoutDate = (EditText) findViewById(R.id.inputWorkoutDate);
inputExceriseName = (EditText) findViewById(R.id.inputExceriseName);
inputSets = (EditText) findViewById(R.id.inputSets);
inputKg = (EditText) findViewById(R.id.inputKg);
inputReps = (EditText) findViewById(R.id.inputReps);
inputNotes = (EditText) findViewById(R.id.inputNotes);
// Create button
Button btnCreateWorkout = (Button) findViewById(R.id.btnCreateWorkout);
// button click event
btnCreateWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new NewWorkoutPlan().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class NewWorkoutPlan extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewWorkout.this);
pDialog.setMessage("Creating Your Workout Plan..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String workout_name = inputWorkoutName.getText().toString();
String workout_date = inputWorkoutDate.getText().toString();
String exercise_name = inputExceriseName.getText().toString();
String sets = inputSets.getText().toString();
String weight_kg = inputKg.getText().toString();
String reps = inputReps.getText().toString();
String notes = inputNotes.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("workout_name", workout_name));
params.add(new BasicNameValuePair("workout_date", workout_date));
params.add(new BasicNameValuePair("exercise_name", exercise_name));
params.add(new BasicNameValuePair("sets", sets));
params.add(new BasicNameValuePair("weight_kg", weight_kg));
params.add(new BasicNameValuePair("reps", reps));
params.add(new BasicNameValuePair("notes", notes));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_workout,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
/**
// successfully created product
Intent i = new Intent(getApplicationContext(), UserProfileActivity.class);
startActivity(i);
//closing this screen
finish();
**/
runOnUiThread(new Runnable() {
@Override
public void run() {
UserWorkoutPlanFragment fd = new UserWorkoutPlanFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, fd);
ft.addToBackStack(null);
ft.commit();
}
});
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
here my UserWorkoutPlanFragment class:
public class UserWorkoutPlanFragment extends ListFragment {
Button CreateWorkout;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> workoutsList;
// url to get all workouts list
private static String url_all_workouts = "http://ec2-54-77-51-119.eu-west-1.compute.amazonaws.com/android_connect/get_all_workouts.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_WORKOUT = "workoutplan";
private static final String TAG_WID = "wid";
private static final String TAG_WORKOUT_NAME = "workout_name";
private static final String TAG_WORKOUT_DATE = "workout_date";
// workouts JSONArray
JSONArray workout = null;
public UserWorkoutPlanFragment(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.user_workoutplan, container, false);
// Buttons
CreateWorkout = (Button) rootView.findViewById(R.id.createworkout);
// create workouts click event
CreateWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching create new workouts activity
Intent i = new Intent(getActivity(), NewWorkout.class);
startActivity(i);
}
});
return rootView;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
//List workout
workoutsList = new ArrayList<HashMap<String, String>>();
// Loading workouts in Background Thread
new LoadAllWorkouts().execute();
// Get listview
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String wid = ((TextView) view.findViewById(R.id.wid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity().getApplicationContext(),
EditWorkouts.class);
// sending pid to next activity
in.putExtra(TAG_WID, wid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllWorkouts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading your workouts, Please wait...");
//pDialog.setMessage("almost done...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_workouts, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Workouts: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Workout
workout = json.getJSONArray(TAG_WORKOUT);
// looping through All Workout
for (int i = 0; i < workout.length(); i++) {
JSONObject c = workout.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_WID);
String name = c.getString(TAG_WORKOUT_NAME);
String date = c.getString(TAG_WORKOUT_DATE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_WID, id);
map.put(TAG_WORKOUT_NAME, name);
map.put(TAG_WORKOUT_DATE, date);
// adding HashList to ArrayList
workoutsList.add(map);
}
} else {
// no workout found
// Launch Add New workout Activity
Intent i = new Intent(getActivity(),
NewWorkout.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all workouts
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter( getActivity(), workoutsList, R.layout.list_item,
new String[] { TAG_WID, TAG_WORKOUT_NAME, TAG_WORKOUT_DATE},
new int[] { R.id.wid, R.id.workout_name , R.id.workout_date});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
Does anyone have any idea how I could resolve this issue?
回答1:
You don't need to extend ListFragment in:
public class UserWorkoutPlanFragment extends ListFragment...
If you do extend ListFragment, you have to reference xml layout containing a ListView element. So the code in onCreateView
pointing to an xml layout:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.user_workoutplan, container, false);
Notes:
- Layout
user_workoutplan
must include a ListView element. Otherwise, extending ListFragment is useless. I think that is the problem. - Again, another solution is to not extend ListFragment.
Good luck in this project. The existing code such at is now is not easy to understand. And there are codes that I think is suspect or should change.
来源:https://stackoverflow.com/questions/30412630/no-view-found-for-id-0x7f0900ad-when-performing-fragment-transaction