继续评价我的功能.. 在这个问题...... 我在片段中写了评价我的功能代码,命名为RateUss ... 但问题是,我没有一个确切的地方在哪里启动那部分代码,然后用户点击导航视图上的按钮速率所以这个速率我对话框将出现
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
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.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class RateUss extends Fragment {
private final static String APP_TITLE = "App Rater";
private final static String APP_PACKAGE_NAME = "com.example.rajafarid.navigation";
private final static int DAYS_UNTIL_PROMPT = 0;
private final static int LAUNCH_UNTIL_PROMPT = 3;
public RateUss() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//app_launched();
return container;}
评估我们定义费率对话框的功能
public static void app_launched(Context context){
SharedPreferences prefs = context.getSharedPreferences("rate_app",0);
if (prefs.getBoolean("dontshowagain", false)){
return;
}
SharedPreferences.Editor editor = prefs.edit();
long launch_count = prefs.getLong("launch_count", 0) + 1;
editor.putLong("launch_count", launch_count);
Long date_firstLaunch = prefs.getLong("date_first_launch", 0);
if (date_firstLaunch ==0){
date_firstLaunch = System.currentTimeMillis();
editor.putLong("date_first_launch", date_firstLaunch);
}
if (launch_count >= LAUNCH_UNTIL_PROMPT) {
if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 *60*60*1000)){
showRateDialog(context, editor);
}
}
editor.commit();
}
public static void showRateDialog(final Context context, final SharedPreferences.Editor editor){
Dialog dialog = new Dialog(context);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
String message = "If you enjoy Using"
+ APP_TITLE
+ ", Please take a moment to rate the app. "
+ "Thank you for your support!";
builder.setMessage(message)
.setTitle("Rate " + APP_TITLE)
.setIcon(context.getApplicationInfo().icon)
.setCancelable(false)
.setPositiveButton("Rate Now", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
editor.putBoolean("dontshowagain", true);
editor.commit();
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PACKAGE_NAME)));
}catch (ActivityNotFoundException e) {
Toast.makeText(context, "You Have pressed Rate Now Button", Toast.LENGTH_SHORT) .show();
}
dialog.dismiss();
}
})
.setNeutralButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "You have Pressed Later Button", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setNegativeButton("No, Thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editor!=null){
editor.putBoolean("dontshowagain", true);
editor.commit();
}
Toast.makeText(context, "You have pressed No, Thanks button", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
}
在这里堆栈跟踪......
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4187)
at android.view.ViewGroup.addView(ViewGroup.java:4040)
at android.view.ViewGroup.addView(ViewGroup.java:3985)
at android.view.ViewGroup.addView(ViewGroup.java:3961)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:2)
您将在container
中返回错误的onCreateView
。你必须创建片段视图并返回它,可能会膨胀一些xml,或者如果片段没有ui则返回null。我建议你阅读:
http://developer.android.com/intl/es/guide/components/fragments.html#Creating