我正在尝试使用以下代码从嵌套片段中启动一个新活动:
Intent rateIntent = new Intent(context, RateServiceActivity.class);
getActivity().startActivity(rateIntent);
我收到以下错误:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.serve.learn/com.serve.learn.MyOrdersPage.RateServiceActivity}:
android.view.InflateException: Binary XML file line #31: Error inflating class fragment
我已尝试过在线提供的所有解决方案,但没有运气。此外,我认为此错误特定于嵌套片段方案,而不是其他帖子中提到的一般错误。
我的xml文件应该没有任何问题,因为到目前为止我没有遇到任何问题。只有当我尝试从嵌套片段中启动新活动时,我才会收到此错误。
无论如何,这是我activity_main.xml
中的代码(从第31行开始):
<fragment
android:id="@+id/navigation_drawer"
android:name="com.serve.learn.MainActivityPage.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
这是嵌套的片段代码:
package com.serve.learn.MyOrdersPage;
import com.serve.learn.R;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Fragment_Fragment_UpcomingOrders extends Fragment implements OnClickListener{
TextView order1Details, order1, order2Details, order2, order3Details, order3;
View rootView;
Button callButton, rateButton;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_fragment_upcoming_orders, container, false);
context = rootView.getContext();
order1Details = (TextView) rootView.findViewById(R.id.order1Details);
order1Details.setVisibility(View.GONE);
order1 = (TextView) rootView.findViewById(R.id.order1);
order1.setOnClickListener(this);
order2Details = (TextView) rootView.findViewById(R.id.order2Details);
order2Details.setVisibility(View.GONE);
order2 = (TextView) rootView.findViewById(R.id.order2);
order2.setOnClickListener(this);
order3Details = (TextView) rootView.findViewById(R.id.order3Details);
order3Details.setVisibility(View.GONE);
order3 = (TextView) rootView.findViewById(R.id.order3);
order3.setOnClickListener(this);
callButton = (Button) rootView.findViewById(R.id.callButton);
callButton.setOnClickListener(this);
rateButton = (Button) rootView.findViewById(R.id.rateButton);
rateButton.setOnClickListener(this);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.order1 : order1Details.setVisibility( order1Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.order2 : order2Details.setVisibility( order2Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.order3 : order3Details.setVisibility( order3Details.isShown()? View.GONE:View.VISIBLE );
break;
case R.id.callButton : Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
break;
case R.id.rateButton : Intent rateIntent = new Intent(context, RateServiceActivity.class);
getActivity().startActivity(rateIntent);
break;
}
}
}
fragment_fragment_upcoming_orders.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal" >
<!-- activity_info layout file -->
<!-- Clickable title -->
<TextView
android:id="@+id/order1"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/Order1" />
<!--content to hide/show -->
<TextView
android:id="@+id/order1Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/Order1Details" />
<TextView
android:id="@+id/order2"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/Order2" />
<TextView
android:id="@+id/order2Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/Order2Details" />
<TextView
android:id="@+id/order3"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="@string/Order3" />
<TextView
android:id="@+id/order3Details"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/Order3Details" />
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="@+id/rateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rate" />
</LinearLayout>
有关我可能出错的地方的任何建议吗?