如何从标签控件启动新活动?

时间:2016-07-02 05:16:48

标签: android android-activity tabcontrol

我有家庭活动,其中有标签控件,在第一个标签上显示主要活动。在主选项卡上,我有按钮,将重定向到另一个活动。 并且无法重定向到另一个活动。

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;

        }
    }
}

5 个答案:

答案 0 :(得分:1)

而不是

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

添加此

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

答案 1 :(得分: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;

答案 2 :(得分:0)

问题可能出在v.getContext()上,所以在声明意图时只需用getActivity()替换它。

答案 3 :(得分: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;
}

答案 4 :(得分: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"/>

这可能有助于你