我建了一个导航抽屉。当应用程序启动时,我将用户直接引导至" MainMenuActivity" MainActivity的活动。 在" MainMenuActivity"内,有一个按钮,应该将用户引导到另一个活动,即" Order List&#34 ;;我应用了两个代码部分,但是非正常工作:
第一个代码:
public class MainMenuActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_mainmenu, container, false);
Button Orderlistbtn = (Button) rootView.findViewById(R.id.orderlistbtn);
Orderlistbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainMenuActivity.this, OrderList.class);
startActivity(intent);
}
});
return rootView;
}
}
应用程序无法运行,第一个代码在logcat中给出了以下错误:
Error:(41, 33) error: no suitable constructor found for Intent(MainMenuActivity,Class<OrderList>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; MainMenuActivity cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; MainMenuActivity cannot be converted to Context)
第二个代码:
public class MainMenuActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_mainmenu, container, false);
Button Orderlistbtn = (Button) rootView.findViewById(R.id.orderlistbtn);
Orderlistbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), OrderList.class);
startActivity(intent);
}
});
return rootView;
}
}
但应用程序在启动时崩溃并出现以下logcat错误
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{package/package.MainMenuActivity}: java.lang.ClassCastException: package.MainMenuActivity cannot be cast to android.app.Activity
这是&#34; activity_mainmenu.xml&#34;文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@android:color/background_light">
<Button
android:background="#ffff8800"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" My order list "
android:id="@+id/orderlistbtn"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
答案 0 :(得分:2)
getActivity() 会返回片段当前与之关联的活动。
Intent intent = new Intent(getActivity(), OrderList.class);
startActivity(intent);
修改强>
其次,您正在 ClassCastException
抛出以指示代码已尝试将对象强制转换为 它不是实例的子类。
答案 1 :(得分:0)
使用以下代码调用下一个活动,希望这可以解决您的问题
Intent intent = new Intent((ParentActivity)getActivity(), NextActivity.class);
(ParentActivity)getActivity().startActivity(myIntent);