문제

내 응용 프로그램에서 폴더를 만들고 여기에 텍스트 파일을 썼습니다.이제 표시해야 합니다.다음은 주요 작업이 시작되는 Java 코드입니다.

private class OpenFileEvent implements OnClickListener
    {

        @Override
        public void onClick(View arg0) {

            LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);

            // TODO Auto-generated method stub
            final Dialog openFileDialog = new Dialog(Notes.this);
            openFileDialog.setTitle("Open File");
            openFileDialog.setContentView(R.layout.open_dialog);


            //First, list all the available Files
            File folder = new File(Environment.getExternalStorageDirectory()+"/Main Notes/Notes/");
            File file = new File(folder.getAbsolutePath());

            File[] fileNameList = file.listFiles();

            try
            {
            if(fileNameList != null && fileNameList.length>0)
            {
                for(int i=0;i<fileNameList.length;i++)
                {
                    //Get the sub views first
                    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                    View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);      
                    Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
                    Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
                    TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);

                    //Set the Text
                    openThisFileNameTxt.setText(fileNameList[i].getName());

                    //Set the Listeners


                    //Add the View
                    openFileDialogView.addView(openThisFileView);

                }
            }
            }
            catch(Exception e)
            {
                Toast.makeText(VoiceNotes.this, "The Error is: "+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            //Show the Dialog
            openFileDialog.show();




        }

    }

여기서는 try..catch 장치에서 실행해야 합니다.그것이 없으면 앱이 단순히 충돌합니다.에뮬레이터에는 필요하지 않습니다.실제 장치에서 오류 메시지는 다음과 같습니다. The Error is: null.에뮬레이터에는 문제가 없습니다.

위의 대화 상자는 다른 레이아웃의 도움으로 생성되었습니다.여기있어.

open_dialog.xml

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


</LinearLayout>

Java 코드에서 볼 수 있듯이 open_dialog.xml 파일 이름과 2개의 버튼을 나타내는 다른 보기로 채워집니다.Here it is에 의해 활성화되는 것입니다.

open_dialog_file.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/open_dialog_file_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/open_this_file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="5dp"
        android:text="TextView" />

    <Button
        android:id="@+id/append_note_this_file"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/open_this_file_button"
        android:text="@string/append_note" />

    <Button
        android:id="@+id/open_this_file_button"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/open" />


    </RelativeLayout>

내 질문은 이것이 실제 장치에서 작동하지 않는 이유입니다.이 코드를 게시하기 전에 이 코드가 찾고 있는 폴더가 존재하는지 여부를 테스트한 결과, 추측한 대로 존재합니다.

실제 장치에서 try catch, 을 클릭하면 제목만 있는 대화 상자가 표시됩니다.그러나 대화 상자는 비어 있습니다.그 외에 유일한 다른 출력은 Toast 메시지.

그렇다면 여기서 문제는 무엇입니까?

도움이 되었습니까?

해결책

문제는 IO에 있는 것이 아닙니다.레이아웃이 팽창되는 것입니다.에뮬레이터에서는 표시할 데이터가 없기 때문에 나타나지 않았으므로 팽창이 없습니다.문제가 다음과 같습니다. LayoutInflater 또는 그와 관련된 것입니다. 폴더 목록을 가져와 외부 보기에 표시하지 않고 토스트에 표시할 수 있었기 때문입니다.

다른 팁

AndroidManifest.xml에 WRITE_EXTERNAL_STORAGE 권한을 넣었습니까?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top