这个问题的标题含糊不清,因为我真的不知道发生了什么。我在名为SummerJobFragment.java
的类中有一个列表视图,这个列表视图有一个onItemClicked()
假设要打开,其他片段叫SummerJobDetailsFragment.java
。下面我发布了代码和logcat截图。
SummerJobFragment.java
public class SummerJobsFragmnet extends Fragment {
public SummerJobsFragmnet() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final MainActivity activity = (MainActivity) getActivity();
String [] places = activity.getAllPositionsNamesPhone().toArray(
new String[activity.getAllPositionsNamesPhone().size()]);
final ListView list = (ListView) getView().findViewById(R.id.joblistView);
int prgmImages=R.mipmap.ic_launcher;
list.setAdapter(new CustomListAdapter(activity,places,prgmImages));
// OnClick listner for the individual cells of the listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SummerJobDetailsFragment.mMyAppsBundle.putInt("value", position);
SummerJobDetailsFragment fragment = new SummerJobDetailsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.layout.displayjobs_detailed, fragment).commit();
}
});
}
SummerJobDetailsFragment.java
public class SummerJobDetailsFragment extends Fragment {
DataBaseHelper summerJobDB;
public static Bundle mMyAppsBundle = new Bundle();
public int position = SummerJobDetailsFragment.mMyAppsBundle.getInt("value");
public SummerJobDetailsFragment() {
// Required empty public constructor
}
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.displayjobs_detailed,container,false);
super.onViewCreated(view, savedInstanceState);
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
DataBaseHelper summerJobDB;
summerJobDB = new DataBaseHelper(getActivity());
Cursor res = summerJobDB.getAllData(position+1);
EditText jobPlace = (EditText)view.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
/*
EditText jobPlace = (EditText)summer.getView().findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
EditText jobPosition = (EditText)summer.getView().findViewById(R.id.jobPlace);
jobPlace.setText(res.getString(2));
EditText starTime = (EditText)summer.getView().findViewById(R.id.jobStartingTime);
jobPlace.setText(res.getString(3));
EditText address = (EditText)summer.getView().findViewById(R.id.jobAddress);
jobPlace.setText(res.getString(5));
EditText phone = (EditText)summer.getView().findViewById(R.id.jobPhoneNum);
jobPlace.setText(res.getString(6));
EditText hours = (EditText)summer.getView().findViewById(R.id.jobHours);
jobPlace.setText(res.getString(4));
*/
return
}
}
这是displayjobs_detail.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:id="@+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/jobplace"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="@+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobStartingTime" />
</LinearLayout>
答案 0 :(得分:0)
在片段的onCreateView中初始化视图项而不是onCreate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
答案 1 :(得分:0)
首先你要在onCreate中进行布局操作,你应该在onCreateView()中这样做:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
EditText jobPlace = (EditText)rootView.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
...
...
}
同时删除此声明
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
没有任何意义,你在rootView上调用findViewById。
不,这就是你的做法,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/displayjobs_detailed"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/jobplace"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="@+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobStartingTime" />
</LinearLayout>
</FrameLayout>
然后在你的片段中使用它来提交
fragmentManager.beginTransaction().replace(R.id.displayjobs_detailed, fragment).commit(); // Note R.id instead of R.layout
答案 2 :(得分:0)
你错了。 Difference between onCreateView and onViewCreated in Fragment
secondView.addSubview(btnPlay)
secondView.bringSubviewToFront(btnPlay)
而不是,打电话
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {