2016-03-15 151 views
0

的速率周转箱的功能。在此 越来越问题... 我已经写过评价我在一个名为作为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) 

回答

2

要退回containeronCreateView这是不对的。您必须创建片段视图并返回该视图,可能会使某些xml膨胀,如果片段没有ui,则返回null。我建议阅读本:

http://developer.android.com/intl/es/guide/components/fragments.html#Creating

+0

片段简化版,有任何UI,所以如果这样的速度片段被打开,但我以前不查看价格我对话......一旦我开始在这个app_launched功能我返回null主要活动,当我运行的应用程序弹出一次后,它不工作...因为它没有一个价格用户界面,所以我不能打电话查看功能.. –

+0

@ farid-raja弹出窗口没有显示直到您打开应用三次,您正在使用变量'LAUNCH_UNTIL_PROMPT'进行管理,您是否有这个想法? – antonicg

+0

@ antonicg..oh三次...好吧让我检查我没有意识到这一点.. –