我正在處理關於Android的在線視頻詛咒。Android - 你能解釋爲什麼你可以刪除代碼嗎?
我有一個項目列表,當我點擊一個列表項目打開一個新的活動來顯示該項目的細節。問題是,我得到詳細的活動的雙重文本,我試圖修復它,我弄清楚,如果我刪除的DetailsActivity#的onCreate()的代碼
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.details_fragment, new DetailsActivityFragment())
.commit();
}
詳細活動顯示的罰款。
這裏是照片和DetailsActivity的
主要活動
詳細活動
但是,如果我(在不同的類片段的工作)的代碼在下面的DetailsActivity#onCreate()中註釋掉部分代碼,詳細活動看起來不錯:
註釋掉的代碼
DetailsActivity.java後:
import android...//all the need package here don't worry about
public class DetailsActivity extends ActionBarActivity {
private Intent shareIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
//Create my Share Intent (for share options in action bar)
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, "My whether data");
shareIntent.setType("text/plain");
/* IF I COMMENT THIS if CODE details showed fine. No double text */
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.details_fragment, new DetailsActivityFragment())
.commit();
}
}
@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_details, menu);
//Locate MenuItem with ShareActionProvider OR find the MenuItem that we know has the ShareActionProvider
MenuItem shareItem = menu.findItem(R.id.action_share_item);
// Fetch and Store ShareActionProvider
ShareActionProvider MyShareProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
if(MyShareProvider != null){
MyShareProvider.setShareIntent(shareIntent);
Log.d(DetailsActivity.class.getSimpleName(), "INTO share Provider--" + MyShareProvider.toString());
}
else{
Log.d(DetailsActivity.class.getSimpleName(), "NO share Provider");
}
return true;
}
....other code...
}
,我必須實現另一大類片段
public class DetailsActivityFragment extends Fragment {
public DetailsActivityFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//get the view of fragment's layout
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
//get Extras form intent
Bundle DataFromIntent = getActivity().getIntent().getExtras();
//get specific data as String.
String weatherDataDetails = DataFromIntent.getString("weatherDataDetails");
//get text view from layout (rootview) and set test on it the above data (whetherData)
((TextView) rootView.findViewById(R.id.details_textview)).setText(weatherDataDetails);
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
}
我的佈局(主+片段)
activity_details.xml(主)
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/details_fragment"
android:name="com.example.android.sunshine.app.DetailsActivityFragment"
tools:layout="@layout/fragment_details"
android:layout_width="match_parent"
android:layout_height="match_parent" />
和fragment_details.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.android.sunshine.app.DetailsActivityFragment">
<TextView
android:text="---null---"
android:id = "@+id/details_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
你能證明你的佈局activity_details.xml? –
當然我會馬上更新我的帖子! – Universe
@DerekFung我更新了我的文章,感謝您的關注 – Universe