2016-04-01 3 views
0

私はDialerアプリケーションを作成していますが、このガイドに従っていますSimple Dialer Application唯一の違いは、Activityアプローチではなく、Fragments(サポートライブラリ付き)を使用していることです。 私はアプリを実行しようとするとうまくコンパイルされますが、普通の白い画面が表示されます。 フラグメントが正しく実装されていないか、何とか無限ループに陥っていますか? 助けてくださいありがとうございます ありがとうございました!ここでPlainアプリケーション実行中の画面

は私の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 

答えて

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(); 
    } 
} 
+0

ありがとうたくさん!それはうまくいった! これらの変更は何を意味していますか教えてください。 – HeroicJokester

+0

@HeroicJokester基本的に 'FragmentTransaction.add(int、fragment、string)'メソッドはフラグメントに「タグ」を追加します。 'findFragmentByTag()'は、指定された* tag *を持つフラグメントのレイアウト( 'setContentView()'で設定されたもの)を検索します。その* tag *を持つフラグメントが見つからない場合、 'null'を返します。これで、新しいフラグメントを作成する前に、フラグメントがヌルであるかどうかを確認できます。 – Bryan

+0

@HeroicJokesterあなたの許可の問題については、あなたの 'targetSdkVersion'は何ですか? 23以上に設定されている場合は、新しい[permissions model](http://developer.android.com/training/permissions/requesting.html)に従い、実行時に権限を要求する必要があります。 – Bryan

関連する問題