2013-05-13 23 views
1

首先让我道歉,这是一段很长的帖子,里面有一段代码,但我想要彻底。Android requestFeature()必须被调用,而不是Dialog

当我点击按钮在我的程序中启动此fragment时,出现logcat错误。这是说明我必须“requestFeature()之前添加内容”。我已经耗尽了这些论坛上的所有资源。我浏览了关于这个主题的所有16篇文章,但这里有一些只是为了向你展示。

error: requestFeature() must be called before adding content - Still won't work

"android.util.AndroidRuntimeException: requestFeature() must be called before adding content" on showDialog(dialogId)

Dialog problem: requestFeature() must be called before adding content

Error: "requestFeature() must be called before adding content", although it is called before setContentView()

这些解决方案的工作

的反复出现的主题与这些职位似乎是dialogs一个问题,customaction等。尝试启动捕获签名的fragment时发生我的错误。相似但不相同。

所以这里是我的代码和我的logcat。

片段

public class CaptureSignature extends Fragment { 

private static final Context Context = null; 
View view; 
LinearLayout mContent; 
signature mSignature; 
Button mClear, mGetSign, mCancel; 
public static String tempDir; 
public int count = 1; 
public String current = null; 
View mView; 
File mypath; 

private String uniqueId; 
private EditText yourName; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    this.getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);  

    view = inflater.inflate(R.layout.fragment_signature, container, false); 

    mClear = (Button) view.findViewById(R.id.clear); 
    mGetSign = (Button) view.findViewById(R.id.getsign); 
    mGetSign.setEnabled(false); 
    mCancel = (Button) view.findViewById(R.id.cancel); 
    yourName = (EditText) view.findViewById(R.id.yourName); 

    uniqueId = getTodaysDate() + "_" + getCurrentTime() + "_" 
      + Math.random(); 
    current = uniqueId + ".png"; 

    mContent = (LinearLayout) view.findViewById(R.id.linearLayout); 
    mSignature = new signature(Context, null); 
    mSignature.setBackgroundColor(Color.WHITE); 
    mContent.addView(mSignature, LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT);  
    mView = mContent;  

    mClear.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Log.v("log_tag", "Panel Cleared"); 
      mSignature.clear(); 
      mGetSign.setEnabled(false); 
     } 
    }); 

    mGetSign.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Log.v("log_tag", "Panel Saved"); 
      boolean error = captureSignature(); 
      if (!error) { 
       mView.setDrawingCacheEnabled(true);           
      } 
     } 
    }); 

    mCancel.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Log.v("log_tag", "Panel Canceled");       
     } 
    }); 
    return view; 

} 

@Override 
public void onDestroy() { 
    Log.w("GetSignature", "onDestory"); 
    super.onDestroy(); 
} 

private boolean captureSignature() { 

    boolean error = false; 
    String errorMessage = ""; 

    if (yourName.getText().toString().equalsIgnoreCase("")) { 
     errorMessage = errorMessage + "Please enter your Name\n"; 
     error = true; 
    } 

    if (error) { 
     Toast toast = Toast 
       .makeText(Context, errorMessage, Toast.LENGTH_SHORT); 
     toast.setGravity(Gravity.TOP, 105, 50); 
     toast.show(); 
    } 

    return error; 
} 

private String getTodaysDate() { 

    final Calendar c = Calendar.getInstance(); 
    int todaysDate = (c.get(Calendar.YEAR) * 10000) 
      + ((c.get(Calendar.MONTH) + 1) * 100) 
      + (c.get(Calendar.DAY_OF_MONTH)); 
    Log.w("DATE:", String.valueOf(todaysDate)); 
    return (String.valueOf(todaysDate)); 

} 

private String getCurrentTime() { 

    final Calendar c = Calendar.getInstance(); 
    int currentTime = (c.get(Calendar.HOUR_OF_DAY) * 10000) 
      + (c.get(Calendar.MINUTE) * 100) + (c.get(Calendar.SECOND)); 
    Log.w("TIME:", String.valueOf(currentTime)); 
    return (String.valueOf(currentTime)); 

}  

public class signature extends View { 
    private static final float STROKE_WIDTH = 5f; 
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH/2; 
    private Paint paint = new Paint(); 
    private Path path = new Path(); 

    private float lastTouchX; 
    private float lastTouchY; 
    private final RectF dirtyRect = new RectF(); 

    public signature(Context captureSignature, AttributeSet attrs) { 
     super(captureSignature, attrs); 
     paint.setAntiAlias(true); 
     paint.setColor(Color.BLACK); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(STROKE_WIDTH); 
    } 



    public void clear() { 
     path.reset(); 
     invalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawPath(path, paint); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float eventX = event.getX(); 
     float eventY = event.getY(); 
     mGetSign.setEnabled(true); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(eventX, eventY); 
      lastTouchX = eventX; 
      lastTouchY = eventY; 
      return true; 

     case MotionEvent.ACTION_MOVE: 

     case MotionEvent.ACTION_UP: 

      resetDirtyRect(eventX, eventY); 
      int historySize = event.getHistorySize(); 
      for (int i = 0; i < historySize; i++) { 
       float historicalX = event.getHistoricalX(i); 
       float historicalY = event.getHistoricalY(i); 
       expandDirtyRect(historicalX, historicalY); 
       path.lineTo(historicalX, historicalY); 
      } 
      path.lineTo(eventX, eventY); 
      break; 

     default: 
      debug("Ignored touch event: " + event.toString()); 
      return false; 
     } 

     invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
       (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
       (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

     lastTouchX = eventX; 
     lastTouchY = eventY; 

     return true; 
    } 

    private void debug(String string) { 
    } 

    private void expandDirtyRect(float historicalX, float historicalY) { 
     if (historicalX < dirtyRect.left) { 
      dirtyRect.left = historicalX; 
     } else if (historicalX > dirtyRect.right) { 
      dirtyRect.right = historicalX; 
     } 

     if (historicalY < dirtyRect.top) { 
      dirtyRect.top = historicalY; 
     } else if (historicalY > dirtyRect.bottom) { 
      dirtyRect.bottom = historicalY; 
     } 
    } 

    private void resetDirtyRect(float eventX, float eventY) { 
     dirtyRect.left = Math.min(lastTouchX, eventX); 
     dirtyRect.right = Math.max(lastTouchX, eventX); 
     dirtyRect.top = Math.min(lastTouchY, eventY); 
     dirtyRect.bottom = Math.max(lastTouchY, eventY); 
    } 
} 
} 

主要活动代码

public class Main extends Activity { 

TabListener<Store_Fragment> surveyTabListener; 
TabListener<Store_Fragment> assetTabListener; 
TabListener<Store_Fragment> installTabListener; 
TabListener<Store_Fragment> punchTabListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState); 

    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.activity_main);  

    // instantiate ActionBar 
    ActionBar actionBar = getActionBar(); 
    //actionBar.setBackgroundDrawable((getResources() 
    //.getDrawable(R.drawable.titlebarheader))); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    actionBar.setTitle("Trakflex"); 
    actionBar.setDisplayShowTitleEnabled(true); 

    // set surveyTab 
    Tab surveyTab = actionBar.newTab(); 
    surveyTabListener = new TabListener<Store_Fragment>(this, 
      R.id.header_fragment_container, Store_Fragment.class);  
    surveyTab.setText("Survey").setContentDescription("Survey Tab") 
      .setTabListener(surveyTabListener); 
    actionBar.addTab(surveyTab); 

    // set assetTab 
    Tab assetTab = actionBar.newTab(); 
    assetTabListener = new TabListener<Store_Fragment>(this, 
      R.id.header_fragment_container, Store_Fragment.class); 
    assetTab.setText("Assets").setContentDescription("Assets Tab") 
      .setTabListener(assetTabListener); 
    actionBar.addTab(assetTab); 

    // set installTab 
    Tab installTab = actionBar.newTab(); 
    installTabListener = new TabListener<Store_Fragment>(this, 
      R.id.header_fragment_container, Store_Fragment.class); 
    installTab.setText("Install Checklist") 
      .setContentDescription("Install Checklist Tab") 
      .setTabListener(installTabListener); 
    actionBar.addTab(installTab); 

    // set punchTab 
    Tab punchTab = actionBar.newTab(); 
    punchTabListener = new TabListener<Store_Fragment>(this, 
      R.id.header_fragment_container, Store_Fragment.class); 
    punchTab.setText("Punchlist").setContentDescription("Punchlist Tab") 
      .setTabListener(punchTabListener); 
    actionBar.addTab(punchTab); 

} 

新的logcat

05-13 18:57:19.767: E/AndroidRuntime(8950): FATAL EXCEPTION: main 
05-13 18:57:19.767: E/AndroidRuntime(8950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facilitysolutionsinc.trackflex/com.facilitysolutionsinc.trackflex.Main}: java.lang.NullPointerException 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.os.Looper.loop(Looper.java:137) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at dalvik.system.NativeStart.main(Native Method) 
05-13 18:57:19.767: E/AndroidRuntime(8950): Caused by: java.lang.NullPointerException 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at com.facilitysolutionsinc.trackflex.Main.onCreate(Main.java:32) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.Activity.performCreate(Activity.java:5104) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
05-13 18:57:19.767: E/AndroidRuntime(8950):  ... 11 more 

如果有人Ç一个帮助,我会非常感谢

回答

4

您正在致电 requestWindowFeature()。这完全是太晚了。正如错误所述,requestFeature() must be called before adding content。在这里,“添加内容”是指您的活动上的setContentView()或运行FragmentTransaction的事情,两者都会在调用FragmentonCreateView()之前发生。

+0

谢谢@CommonsWare,我知道我可以相信你对这个问题有很好的理解。那么我应该问一下,何时/应该在哪里调用requestWindowFeature()?在我的主要活动/ ParentActivity? – IrishWhiskey 2013-05-13 22:42:15

+2

@IrishWhiskey:“何时/应该在哪里调用requestWindowFeature()?” - 作为'Activity'的'onCreate()'中的第一个语句。我甚至在'super.onCreate()'之前看到过它的调用,特别是如果超类自身通过处理内容视图(例如'ListActivity')。 – CommonsWare 2013-05-13 22:43:26

+0

嗯,我试图把它放在我的'活动'中。在'super'之后,'view'之前的'super'被夸大之前。他们都从现在开始就让我的程序崩溃。我现在有一个'NullPointerException'错误。 **叹**。我已经厌倦了这个问题,而且我完全不了解 – IrishWhiskey 2013-05-13 22:50:12

相关问题