我遇到了來自Android的片段的強化問題。 我有一個活動,持有一個片段,它反過來持有一個ListView。 我已經註冊了一個onItemClickListener到ListView,並且當我在活動中擁有listView權限時,這很好。然而,我需要將它包裝在片段中。意圖不啓動從片段的活動
因此,我不打電話getContext()
現在撥打電話getActivity()
隨處可見。這實際上會返回正確的活動。 點擊也被註冊,因爲我的LogCat正在打印。但意圖從未開始。什麼都沒發生。 有誰能告訴我我錯過了什麼嗎?
這裏是我的代碼:
_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent in = new Intent(getActivity(), DealView.class);
in.putExtra("deal", position);
in.putExtra("city", _citySearchBar.getText());
startActivity(in);
Log.d("BASE_FRAGMENT", "Activity should have been started here");
}
});
}
此代碼存在一個抽象類中,但它移動到子類不有所作爲。
這是DealView類的意圖旨在:
public class DealView extends AppCompatActivity {
private TextView _priceView;
private TextView _descriptionView;
private TextView _titleView;
private TextView _storeView;
private ImageView _imageView;
private String _cityName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deal_view);
Intent fromDealSelection = getIntent();
_cityName = fromDealSelection.getStringExtra("city");
//Initializing the different components of the view
_priceView = (TextView) findViewById(R.id.priceInputDealView);
_descriptionView = (TextView) findViewById(R.id.dealDescriptionDealView);
_titleView = (TextView) findViewById(R.id.dealTitleDealView);
_storeView = (TextView) findViewById(R.id.storeDealView);
_imageView = (ImageView) findViewById(R.id.imageViewDealView);
_cityName = fromDealSelection.getStringExtra("city");
}
這裏是logcat的輸出:
03/01 09:42:23: Launching app
W/System: ClassLoader referenced unknown path: /data/data/de.coding.mb.konstisapp/lib
Hot swapped changes, activity restarted
D/BASE_FRAGMENT: Activity should have been started here
D/BASE_FRAGMENT: Activity should have been started here
這裏是我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.coding.mb.konstisapp">
<uses-feature
android:name="android.hardware.camera"
android:required="false"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.GreatingsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".activities.DealSelectionActivity"
android:label="@string/title_activity_deal_selection"
android:parentActivityName=".activities.GreatingsActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.GreatingsActivity"/>
</activity>
<activity
android:name=".activities.DealCreationActivity"
android:label="@string/title_activity_activity_deal_creation"
android:parentActivityName=".activities.DealSelectionActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.DealSelectionActivity"/>
</activity>
<activity android:name=".activities.DealView"
android:label="@string/title_activity_deal_view"
android:parentActivityName=".activities.DealSelectionActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.coding.mb.konstisapp.activities.DealSelectionActivity">
</meta-data>
</activity>
</manifest>
有你添加活動清單文件..? – Keerthivasan
你可以顯示你的logcat嗎? – TruongHieu
getContext()使用此 –