我知道这个问题已存在于此论坛中,但我创立的每个解决方案都没有奏效。我想从非Activity类启动一个Activity。非Activity类是Navigation-Drawer的DetailFragment.java
DetaiFragment:
package com.developing.konstantin.besmart;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DetailFragment extends Fragment {
FrameLayout fLayout;
View view;
public DetailFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
String menu = getArguments().getString("Menu");
switch (menu) {
case ("Home"): {
fLayout = (FrameLayout) view.findViewById(R.id.home) ;
fLayout.setVisibility(View.VISIBLE);
break;
}
case ("Info"): {
fLayout = (FrameLayout) view.findViewById(R.id.info) ;
fLayout.setVisibility(View.VISIBLE);
break;
}
case ("Video"): {
break;
}
}
return view;
}
}
我想在第三种情况下启动活动("视频")。我怎么能这样做?
答案 0 :(得分:5)
您可以使用context
:
inflater.getContext()
为:
startActivity(new Intent(inflater.getContext(), Video.class));
或者您可以使用getActivity()
方法,例如:
startActivity(new Intent(getActivity(), Video.class));
答案 1 :(得分:0)
这取决于您呼叫活动的位置。
最好创建
的实例Context context;
并将其初始化为
context = AnyActivity.this; // if you are calling from Activity
context = getActivity(); // if you are using Fragments
并称之为
Intent intent = new Intent(context,ActivityToOpen.class);
startActivity(intent);