2016-07-02 45 views
0

我有家庭活動有選項卡控制,並在第一個選項卡上顯示主要活動。在主選項卡上,我有按鈕將重定向到另一個活動。 ,並且無法重定向到其他活動。如何從標籤控件開始新的活動?

public class Main1 extends Fragment 
{ 
    ImageView saving, loan; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     saving = (ImageView) container.findViewById(R.id.imgSavingCollection); 
     loan = (ImageView) container.findViewById(R.id.imgLoanCollection); 

     saving.setOnClickListener((View.OnClickListener) container); 
     loan.setOnClickListener((View.OnClickListener) container); 

     View v = inflater.inflate(R.layout.activity_main1, container, false); 
     return v; 
    } 

    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.imgSavingCollection: 
       Intent savingactivity = new Intent(v.getContext(), SavingCollection.class); 
       startActivity(savingactivity); 
       break; 

     } 
    } 
} 
+0

發佈您的代碼在這裏 –

+0

當我運行應用程序時自動停止活動。 – Gopal

+1

顯示你的logcat –

回答

0

試圖改變你的代碼像替換下面的代碼,

public class Main1 extends Fragment 
{ 
    ImageView saving, loan; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.activity_main1, container, false); 

    saving = (ImageView) rootView.findViewById(R.id.imgSavingCollection); 
    loan = (ImageView) rootView.findViewById(R.id.imgLoanCollection); 

saving.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent savingactivity = new Intent(getActivity(), SavingCollection.class); 
      startActivity(savingactivity); 
     } 
    }); 

return rootView; 
} 

} 

檢查您manifest.xml

<activity android:name=".SavingCollection"/> 

這可能會幫助你

0

使用此

Intent savingactivity = new Intent(getActivity(), SavingCollection.class); 
startActivity(savingactivity); 

更換v.getContext()getActivity()

,並定義輪廓線的上側

View v = inflater.inflate(R.layout.activity_main1, container, false);

全碼:

View v = inflater.inflate(R.layout.activity_main1, container, false); 

saving = (ImageView) container.findViewById(R.id.imgSavingCollection); 
    loan = (ImageView) container.findViewById(R.id.imgLoanCollection); 

    saving.setOnClickListener((View.OnClickListener) container); 
    loan.setOnClickListener((View.OnClickListener) container); 


    return v; 
+0

仍然錯誤正在處理進程:mobileofflineapp.gopalchudal.mobileofflineapp,PID:2425 java.lang.ClassCastException:android.support.v4.view.ViewPager無法轉換爲android.view.View $ OnClickListener at mobileofflineapp.gopalchudal.mobileofflineapp .Main1.onCreateView(Main1.java:21) android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) – Gopal

+0

您的初始化可能會做錯誤的方式發佈您的XML代碼在這裏。 – Ironman

0

問題可能是v.getContext(),所以只需getActivity取代它()當你宣佈意圖。

0

有問題你onCreateView,你onCreateView

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.activity_main1, container, false); 
    saving = (ImageView) v.findViewById(R.id.imgSavingCollection); 
    loan = (ImageView) v.findViewById(R.id.imgLoanCollection); 

    saving.setOnClickListener((View.OnClickListener) container); 
    loan.setOnClickListener((View.OnClickListener) container); 


    return v; 
} 
1

代替

Intent savingactivity = new Intent(v.getContext(), SavingCollection.class); 
startActivity(savingactivity); 

添加此

Intent savingactivity = new Intent(getActivity(), SavingCollection.class); 
startActivity(savingactivity);