在我的片段類中,我有一些按鈕,所以當我點擊那些按鈕時,我想重定向到另一個Activity類。我如何在片段類中使用重定向?如何使用Fragment類中的意圖重定向到另一個類?
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);
}
}
最終按鈕backBtn =(按鈕)findViewById(R.id.button3); –
這裏得到「方法findViewById(int)未定義的類型ServiceFragment」 –
因爲你應該有'最後的按鈕backBtn =(Button)rootView.findViewById(R.id.button3);' –