运行应用程序时屏幕正常

时间:2016-04-01 11:41:35

标签: java android android-fragments

我正在制作拨号器应用程序并且我正在遵循本指南Simple Dialer Application唯一的区别是我使用的是Fragments(带有支持库)而不是Activity方法。 当我尝试运行应用程序时,它编译得很好但显示纯白色屏幕。 片段没有正确实现,或者我以某种方式进入无限循环? 请任何帮助是赞赏的 在此先感谢!

这是我的MainActivity.java:

package com.heroicjokester.android.haid;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class DialerActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialer);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = new DialerFragment();

        if(fragment == null){
            fragment = new DialerFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container,fragment)
                    .commit();
        }
}
}

我的activity_dialer.xml:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

</FrameLayout>

Fragment_dialer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

<EditText
    android:id="@+id/input_pno"
    android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/input_pno_hint"
    />

<Button
    android:id="@+id/dial_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dial_button"
    android:layout_gravity="center_horizontal"/>



</LinearLayout>

DialerFragment.java:

 package com.heroicjokester.android.haid;

 import android.Manifest;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;;
 import android.support.v4.content.ContextCompat;

/**
  * Created by Ripcord on 01-Apr-16.
  */
 public class DialerFragment extends Fragment {
 private EditText mPhoneField;
 private Button mDialButton;

//Requesting Permissions using Runtime Permissions.
final private int REQUEST_CODE_ASK_PERMISSIONS=0;

private void InitiateCall(){
    int hasCallPermission =          ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_PHONE_STATE);
    if (hasCallPermission != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
    InitiateCall();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults ){
   switch (requestCode){
       case REQUEST_CODE_ASK_PERMISSIONS:
           if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
               //YAY! PERMISSION GRANTED
               InitiateCall();
           }else{
               //GD! PERMISSION DENIED
               Toast.makeText(getActivity(), R.string.permission_denied, Toast.LENGTH_SHORT).show();
           }
            break;
       default:
           super.onRequestPermissionsResult(requestCode, permissions, grantResults);

   }
}



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

}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    View v=inflater.inflate(R.layout.fragment_dialer,container,false);

    mPhoneField=(EditText) v.findViewById(R.id.input_pno);
    mDialButton=(Button) v.findViewById(R.id.dial_button);

    mDialButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            try{
                if (mPhoneField != null && (mPhoneField.getText().length()==10||mPhoneField.getText().length()==11)){
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhoneField.getText())));
                }
                else if(mPhoneField != null && mPhoneField.getText().length()==0){
                    Toast.makeText(getActivity(),R.string.no_number_toast,Toast.LENGTH_SHORT).show();
                }
                else if(mPhoneField !=null && mPhoneField.getText().length()<10){
                    Toast.makeText(getActivity(),R.string.wrong_number_toast,Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e){
                Log.e("DialerAppActivity","error: " + e.getMessage(),e);//Runtime error will be logged
            }
        }
    });


    return v;
}

}

最后我的日志文件:

 04-01 16:55:56.067 2372-2372/? I/art: Not late-enabling -Xcheck:jni (already on)
 04-01 16:55:56.143 2372-2372/com.heroicjokester.android.haid W/System: ClassLoader referenced unknown path: /data/app/com.heroicjokester.android.haid-1/lib/x86
 04-01 16:55:56.173 2372-2385/com.heroicjokester.android.haid D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
 04-01 16:55:56.279 2372-2385/com.heroicjokester.android.haid I/OpenGLRenderer: Initialized EGL, version 1.4
 04-01 16:55:56.564 2372-2385/com.heroicjokester.android.haid W/EGL_emulation: eglSurfaceAttrib not implemented
 04-01 16:55:56.564 2372-2385/com.heroicjokester.android.haid W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfebbe0, error=EGL_SUCCESS
 04-01 16:55:57.263 2372-2372/com.heroicjokester.android.haid I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
 04-01 17:01:31.638 2372-2378/com.heroicjokester.android.haid W/art: Suspending all threads took: 9.827ms
 04-01 17:06:24.378 2372-2378/com.heroicjokester.android.haid W/art: Suspending all threads took: 15.026ms

1 个答案:

答案 0 :(得分:0)

if(fragment == null)永远不会返回true,因此永远不会调用您的片段事务。我假设您正在尝试确保片段在方向更改时不会被替换,在这种情况下,您应该首先尝试找到片段,如果找不到片段,请创建一个新片段:

private static final String FRAGMENT_TAG_DIALER = "fragment:dialer";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialer);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = (DialerFragment) fm.findFragmentByTag(FRAGMENT_TAG_DIALER);

    if(fragment == null){
        fragment = new DialerFragment();
        fm.beginTransaction()
                .add(R.id.fragment_container, fragment, FRAGMENT_TAG_DIALER)
                .commit();
    }
}