2017-03-01 75 views
0

我遇到了來自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> 
+0

有你添加活動清單文件..? – Keerthivasan

+1

你可以顯示你的logcat嗎? – TruongHieu

+0

getContext()使用此 –

回答

2

由於Fragment中新的Activity的啓動不起作用,您可以嘗試將代碼遷移到託管活動。一個乾淨且可重用的方法是在一個新文件中創建一個接口。

public interface myInterface { 
    public void startMyIntent(Intent i); 
} 

然後,實現Activity類此接口,承載您的片段

public class hostingActivity extends AppCompatActivity implements myInterface { 
    @Override 
    public void startMyIntent(Intent i) { 
     startActivity(i); 
    } 
} 

在你的碎片的onItemClickListener,你可以這樣調用

_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     { 
      Activity parentActivity = getActivity(); 
      Intent in = new Intent(parentActivity, DealView.class); 
      in.putExtra("deal", position); 
      in.putExtra("city", _citySearchBar.getText()); 
      ((myInterface)parentActivity).startMyIntent(in); 
      Log.d("BASE_FRAGMENT", "Activity should have been started here"); 
     } 
    }); 
} 
+0

非常感謝!不知道爲什麼其他方法不起作用,但這是它 – Timbo

+0

@Timbo歡迎您!除了工作,這似乎成爲Fragments做'活動的東西'的最佳解決方案,也許這個約定來自像你這樣的問題;) – EarlGrey

+0

@Timbo當我再次查看這個答案時,我決定將引用保存在本地變量的父Activity中,如它需要兩次。在這種情況下並不是真的有必要,但在我眼中這是更好的做法。 – EarlGrey

0

從片段內部,你不能只使用startActivity()
你必須使用getActivity().startActivity(in)

_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()); 
      getActivity().startActivity(in); 
      Log.d("BASE_FRAGMENT", "Activity should have been started here"); 
     } 
    }); 
} 

又見How do I start an activity from within a Fragment?

+0

我認爲這是正確的答案。 OP應該檢查這一點。 – TruongHieu

+0

剛剛檢查。這裏也沒有任何反應。我也看過這篇文章,昨天試了一下 – Timbo

0

改變的代碼

_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
      { 
       Intent in = new Intent(v.getContext(), DealView.class); 
       in.putExtra("deal", position); 
       in.putExtra("city", _citySearchBar.getText()); 
       startActivity(in); 
       Log.d("BASE_FRAGMENT", "Activity should have been started here"); 
      } 
     }); 
    } 
+1

這也行不通:(我不知道爲什麼它不會啓動活動 另外v.getContext()和getActivity()返回相同的參考 – Timbo

0

使用intent.setFlags(...)方法FLAG_ACTIVITY_NEW_TASK。

_dealList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     { 
      Intent in = new Intent(v.getContext(), DealView.class); 
      in.putExtra("deal", position); 
      in.putExtra("city", _citySearchBar.getText()); 
      in.setFlag(Acitvity.FLAG_ACTIVITY_NEW_TASK); 
      getActivity().startActivity(in); 
      Log.d("BASE_FRAGMENT", "Activity should have been started here"); 
     } 
    }); 
} 
+0

這個標誌只有影響開始新的活動*如果它啓動的處理 – EarlGrey