質問

私のアプリケーションでは、フォルダを作成してテキストファイルを書きました。さて、私はそれらを表示する必要があります。以下は、主な操作が始まる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つのボタンを表します。ここで活性化されるものです。

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にはありません。レイアウトで膨らませることです。エミュレータでは、表示されるデータがないため、膨らんでいません。私は、将来のビューを表示せずに任意のフォルダのリストを取得してトーストに表示することができたため、問題に関連するものであるという問題が発見されました。

他のヒント

write_external_storage権限をandroidmanifest.xml:

にしましたか?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top