【问题标题】:How can i use intent in Fragment class for redirecting to another class?如何在 Fragment 类中使用意图重定向到另一个类?
【发布时间】:2015-09-10 08:01:07
【问题描述】:

在我的片段类中,我有一些按钮,所以当我单击这些按钮时,我想重定向到另一个 Activity 类。如何在片段类中使用重定向?

fragment_services.xml `

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="409dp" >
<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="160dp"
    android:layout_height="90dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="18dp"
    android:background="@drawable/cloud" />
 </RelativeLayout>
 </ScrollView>
 </RelativeLayout>`

ServiceFragment.java

public class ServiceFragment extends Fragment {
private Context CurrentObj=this;
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
 {
     View rootView = inflater.inflate(R.layout.fragment_services, container,   false);
     final Button backBtn=(Button)findViewById(R.id.button3);
     backBtn.setOnClickListener(new OnClickListener()
     {
            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                Intent redirect=new Intent(CurrentObj.getApplicationContext(),Cloud.class);
                startActivity(redirect);
            }
        });
     return rootView;
 }

}

Cloud.java

public class Cloud extends Activity{
private Context CurrentObj=this;
@Override

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cloud);
}

}

【问题讨论】:

    标签: android android-fragments android-intent fragment


    【解决方案1】:
    backBtn.setOnClickListener(new OnClickListener()
     {
            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                Intent redirect=new Intent(getActivity(),Cloud.class);
                getActivity().startActivity(redirect);
            }
        });
    

    您还需要确保您的 Cloud 类正在扩展 Activity,并且您已在 AndroidManifest.xml 中声明了您的 Cloud Activity。

    我还在您的代码中发现了一些小错误。 ServiceFragment 类中的这一行 private Context CurrentObj=this; 将导致编译器错误,因为 Fragments 不是 Context 或 ContextWrappers 的子类。您需要从 Fragment 更改或删除此行。

    【讨论】:

    • 最终按钮 backBtn=(Button)findViewById(R.id.button3);
    • 这里出现“ServiceFragment 类型的方法 findViewById(int) 未定义”
    • 因为你应该有final Button backBtn=(Button)rootView.findViewById(R.id.button3);
    • 单击按钮时出现运行时异常“java.lang.NullPointerException”
    • @BipinD 如果我有帮助,请检查我的回答是否已接受。
    【解决方案2】:

    试试这个getActivity().startActivity(redirect);

    【讨论】:

      【解决方案3】:

      你不应该忘记你的父母活动。

      Intent redirect=new Intent(getActivity(),Cloud.class);
                          getActivity().startActivity(redirect);
      

      【讨论】:

        【解决方案4】:

        如果您想使用上下文来执行此操作,请执行以下操作

        private Context CurrentObj=getActivity();
        
        backBtn.setOnClickListener(new OnClickListener()
         {
                @Override
                public void onClick(View arg0)
                {
                    // TODO Auto-generated method stub
                    Intent redirect=new Intent(CurrentObj,Cloud.class);
                    CurrentObj.startActivity(redirect);
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-05
          • 1970-01-01
          • 2021-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多