2012-07-22 12 views
5

私はTextViewをTextファイルからロードするように設定しようとしていました。私は大丈夫作業空のTextViewを使用して、この基本的なTextSwitcher例があります。xmlからTextSwitcherへのTextViewのロード

.java 

public class TextSwitcherTest extends Activity 
{ 
    private TextSwitcher textSwitcher; 

    private ViewFactory viewFactory = new ViewFactory() 
    { 
     public View makeView() 
     { 
      TextView textView = new TextView(TextSwitcherTest.this); 

      return textView; 
     } 
    }; 

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

     setContentView(R.layout.main); 

     textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); 

     textSwitcher.setFactory(viewFactory); 

     Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in); 
     Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out); 

     textSwitcher.setInAnimation(in); 
     textSwitcher.setOutAnimation(out); 

     textSwitcher.setText("test ok"); 
    }  
} 

-

main.xml 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextSwitcher 
     android:id="@+id/textSwitcher" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

しかし、私は試してみて、XMLファイルからのTextViewをロードするようにコードを変更すると、それエラーの山が吐き出される。ここで修正されたコードは次のとおりです。

.java 

public class TextSwitcherTest extends Activity 
{ 
    private TextSwitcher textSwitcher; 

    private ViewFactory viewFactory = new ViewFactory() 
    { 
     public View makeView() 
     { 
      LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this); 

      TextView textView = (TextView) inflater.inflate(R.id.textView,null); 

      return textView; 
     } 
    }; 

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

     setContentView(R.layout.main); 

     textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); 

     textSwitcher.setFactory(viewFactory); 

     Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in); 
     Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out); 

     textSwitcher.setInAnimation(in); 
     textSwitcher.setOutAnimation(out); 

     textSwitcher.setText("test ok"); 
    }  
} 

-

textview.xml 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

誰もが私が間違っているつもりだ提案を提供することはできますか?コードは大丈夫ですが、LayoutInflaterが何らかの方法で悪用されていると思いますか?


エラーメッセージ(それは'11以上が表示されませんでした):

まずLayoutInflater.inflate()方法にidを期待し、:

07-22 03:51:24.096: W/dalvikvm(580): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
07-22 03:51:24.137: E/AndroidRuntime(580): FATAL EXCEPTION: main 
07-22 03:51:24.137: E/AndroidRuntime(580): java.lang.RuntimeException: Unable to start activity ComponentInfo{test09.TextSwitcher01/test09.TextSwitcher01.TextSwitcherTest}: android.content.res.Resources$NotFoundException: Resource ID #0x7f050001 type #0x12 is not valid 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.os.Looper.loop(Looper.java:137) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread.main(ActivityThread.java:4424) 
07-22 03:51:24.137: E/AndroidRuntime(580): at java.lang.reflect.Method.invokeNative(Native Method) 
07-22 03:51:24.137: E/AndroidRuntime(580): at java.lang.reflect.Method.invoke(Method.java:511) 
07-22 03:51:24.137: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-22 03:51:24.137: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-22 03:51:24.137: E/AndroidRuntime(580): at dalvik.system.NativeStart.main(Native Method) 
07-22 03:51:24.137: E/AndroidRuntime(580): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050001 type #0x12 is not valid 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2110) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.content.res.Resources.getLayout(Resources.java:857) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
07-22 03:51:24.137: E/AndroidRuntime(580): at test09.TextSwitcher01.TextSwitcherTest$1.makeView(TextSwitcherTest.java:23) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.widget.ViewSwitcher.obtainView(ViewSwitcher.java:80) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:99) 
07-22 03:51:24.137: E/AndroidRuntime(580): at test09.TextSwitcher01.TextSwitcherTest.onCreate(TextSwitcherTest.java:38) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.Activity.performCreate(Activity.java:4465) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
07-22 03:51:24.137: E/AndroidRuntime(580): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
07-22 03:51:24.137: E/AndroidRuntime(580): ... 11 more 
+0

「エラーの山」を追加してください。 – Shine

+0

ok、申し訳ありません、私は以前にそれらを含めていませんでした! – Spoonface

答えて

14

あなたのコードと間違っていくつかの事がありますR.layout.layout_nameの形式ではなく、R.id.some_idの形式ではありません。

TextView textView = (TextView) inflater.inflate(R.layout.textView, null); 

第2に、膨張したレイアウトのルートがLinearLayoutであり、キャストしようとするとTextViewではないため、使用するコードはClassCastExceptionです。あなたのコードは次のようになります:

LinearLayout ll =(Linearlayout)inflater.inflate(R.layout.textView、null);

しかし示唆その名の通り、TextSwitcherはタイプTextViewの子だけがかかります、何もTextSwitcherと2人の子供TextViewの間で来ることはできない、ためにも上記の行は動作しません。使用する正しいコードは最後に次のようになります。textview.xmlため

レイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

そしてviewFactoryフィールドは次のようになります。

private ViewFactory viewFactory = new ViewFactory() { 
     public View makeView() { 
      LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this); 
      TextView textView = (TextView) inflater.inflate(R.layout.textView, null); 
      return textView; 
     } 
}; 
+0

こんにちは、私は今理解しています。あなたの時間をありがとう、ありがとう! – Spoonface

+0

はrootviewがnullであるべきですか? –

+1

@DennisAnderson膨大なビューをrootViewに添付したくない場合(基本的に、(インフレの瞬間にレイアウトを別のビューに貼り付けることなくレイアウトを膨張させたいだけの場合)rootViewはnullにする必要があります。 inflate()メソッドのドキュメンテーションを見てください。このメソッドはブール値のパラメータ 'https://developer.android.com/reference/android/view/LayoutInflater.html#inflate(org.xmlpull.v1.XmlPullParser、android)を受け取ります。 .view.ViewGroup、boolean) ' – Luksprog

0

私はこの解決策を考え出しました私にとってTextViewをXMLで直接定義するのに最も近いのは私です:

まず、TextSwitcherを2つのTextViewで定義します。カスタム定義とその内部bjects、:その後

 <TextSwitcher 
      android:fontFamily="sans-serif-condensed" 
      android:id="@+id/detail_date_textswitch" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent"> 
      <TextView 
       android:id="@+id/detail_date_textview1" 
       android:textColor="@color/grey_700" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="20sp"/> 
      <TextView 
       android:id="@+id/detail_date_textview2" 
       android:textColor="@color/grey_700" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="20sp"/> 
     </TextSwitcher> 

は、あなたのコードでは、あなたのTextSwitcherオブジェクトに対して次のファクトリを定義:

textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { 
     boolean isFirst = true; 
     @Override 
     public View makeView() { 
      if (isFirst) { 
       isFirst = false; 
       TextView textView = (TextView) mDateView.findViewById(R.id.detail_date_textview1); 
       mDateView.removeViewAt(0); 
       return textView; 
      } else { 
       TextView textView = (TextView) mDateView.findViewById(R.id.detail_date_textview2); 
       mDateView.removeViewAt(0); 
       return textView; 
      } 
     } 
    }); 

は、私はそれが一種のトリッキーではありません言うことはありません、それは私のために働いた。

1

実際には、レイアウトXMLのTextSwitcherTextViewsの2つを入れると、工場を設定する必要はありません。これが私の仕事:

<TextSwitcher 
    android:id="@+id/id1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inAnimation="@android:anim/fade_in" 
    android:outAnimation="@android:anim/fade_out"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:lines="1"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:lines="1"/> 
</TextSwitcher> 

(追加の設定は、コード内で行われていないだけでinflatefindViewById

は、しかし、あなたがTextViews全体の属性を複製する必要があります。これは、別のファイルにTextViewを移動して、それを2回含むことによって回避される可能性があります。

<TextSwitcher 
    android:id="@+id/id1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inAnimation="@android:anim/fade_in" 
    android:outAnimation="@android:anim/fade_out"> 

    <include layout="@layout/titlebar" /> 
    <include layout="@layout/titlebar" /> 
</TextSwitcher> 

重複がカスタムスタイルを作成し、両方のTextViewsに適用されるだろう回避するための他の方法を。

+0

よくできました!これは最もクリーンな方法です。 – Josselin