2012-02-15 119 views
-1

我在我的项目中使用ViewPager,并且在设置同一布局上的不同数据时遇到了一些问题。所以我需要在我的textViews上使用setText来定制每个页面。 但我不能在包含该TextView的ViewGroup充气后在TextView中设置TextText()。 下面的代码:如果删除setText()线setText()膨胀后

public class ScheduleActivity extends Activity { 

    private List<View> mPages; 
    private ViewPager mPager; 
    private TitlePageIndicator mTitleIndicator; 
    private ArrayList<String> titles; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.pages); 
     initUi(); 
    } 

    private void initUi() { 
     LayoutInflater inflater = LayoutInflater.from(this); 
     mPages = new ArrayList<View>(); 
     titles = new ArrayList<String>(); 

     titles.add(getString(R.string.monday)); 
     titles.add(getString(R.string.tuesday)); 
     titles.add(getString(R.string.wednesday)); 
     titles.add(getString(R.string.thursday)); 
     titles.add(getString(R.string.friday)); 
     titles.add(getString(R.string.saturday)); 

     for (String title : titles) { 

      TextView[] text = new TextView[7]; 
      text[0] = (TextView) findViewById(R.id.text1); 
      text[1] = (TextView) findViewById(R.id.text2); 
      text[2] = (TextView) findViewById(R.id.text3); 
      text[3] = (TextView) findViewById(R.id.text4); 
      text[4] = (TextView) findViewById(R.id.text5); 
      text[5] = (TextView) findViewById(R.id.text6); 
      text[6] = (TextView) findViewById(R.id.text7); 

      for (int i = 0; i < 7; i++) { 
       text[i].setText(R.string.monday); 
      } 

      View day = inflater.inflate(R.layout.schedule, null); 

      day.setTag(title); 
      mPages.add(day); 
     } 

     MainPageAdapter adapter = new MainPageAdapter(mPages); 
     mPager = (ViewPager) findViewById(R.id.pager); 
     mPager.setAdapter(adapter); 
     mPager.setCurrentItem(0); 

     mTitleIndicator = (TitlePageIndicator) findViewById(R.id.indicator); 
     mTitleIndicator.setViewPager(mPager); 
     mTitleIndicator.setCurrentItem(0); 
     final float density = getResources().getDisplayMetrics().density; 
     mTitleIndicator.setTextSize(15 * density); // 15dp 
     mTitleIndicator.setFooterLineHeight(1 * density); // 1dp 
     mTitleIndicator.setFooterIndicatorHeight(3 * density); // 3dp 
     mTitleIndicator.setFooterIndicatorStyle(IndicatorStyle.Underline); 
     mTitleIndicator.setFooterColor(0x34B4E3); 
     mTitleIndicator.setTextColor(0xAA000000); 
     mTitleIndicator.setSelectedColor(0xFF000000); 
     mTitleIndicator.setSelectedBold(true); 
    } 
} 

一切工作好了。这里的LogCat:

02-15 18:17:54.294: D/AndroidRuntime(465): Shutting down VM 
02-15 18:17:54.323: W/dalvikvm(465): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
02-15 18:17:54.353: E/AndroidRuntime(465): FATAL EXCEPTION: main 
02-15 18:17:54.353: E/AndroidRuntime(465): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.stiggpwnz.schedule/com.android.stiggpwnz.schedule.ScheduleActivity}: java.lang.NullPointerException 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.os.Looper.loop(Looper.java:123) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-15 18:17:54.353: E/AndroidRuntime(465): at java.lang.reflect.Method.invokeNative(Native Method) 
02-15 18:17:54.353: E/AndroidRuntime(465): at java.lang.reflect.Method.invoke(Method.java:521) 
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-15 18:17:54.353: E/AndroidRuntime(465): at dalvik.system.NativeStart.main(Native Method) 
02-15 18:17:54.353: E/AndroidRuntime(465): Caused by: java.lang.NullPointerException 
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.stiggpwnz.schedule.ScheduleActivity.initUi(ScheduleActivity.java:63) 
02-15 18:17:54.353: E/AndroidRuntime(465): at com.android.stiggpwnz.schedule.ScheduleActivity.onCreate(ScheduleActivity.java:36) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-15 18:17:54.353: E/AndroidRuntime(465): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-15 18:17:54.353: E/AndroidRuntime(465): ... 11 more 

第63行是setText()行。

UPD:改成了这个样子,但还是同样的异常:

day = inflater.inflate(R.layout.schedule, null); 
    day.findViewById(R.layout.schedule); 

    for (String title : titles) { 

     TextView[] text = new TextView[7]; 
     text[0] = (TextView) findViewById(R.id.text1); 
     text[1] = (TextView) findViewById(R.id.text2); 
     text[2] = (TextView) findViewById(R.id.text3); 
     text[3] = (TextView) findViewById(R.id.text4); 
     text[4] = (TextView) findViewById(R.id.text5); 
     text[5] = (TextView) findViewById(R.id.text6); 
     text[6] = (TextView) findViewById(R.id.text7); 

     for (int i = 0; i < 7; i++) { 
      text[i].setText(R.string.monday); 
     } 

     day.setTag(title); 
     mPages.add(day); 
    } 

UPD 2:这是怎么开始工作:

View day = inflater.inflate(R.layout.schedule, null); TextView tv = (TextView)day.findViewById(R.id.text1); 
+0

你还可以向你展示布局文件,你有这些textviews。 – Vikram 2012-02-15 15:33:00

+0

Question为什么要创建TextView [] text = new TextView [7];并循环通胀?我没有看到循环内的第二个循环的意义...... – 2012-02-15 15:34:16

+0

b'cos我稍后需要该循环。我刚刚得到了问题所在。我没有在我的主布局(R.layout.pages)这些textViews,但我有他们在我的其他布局,我膨胀(R.layout.schedule)。所以我现在需要的是如何引用不在我的布局中的TextView,而是在我膨胀的布局中。 – 2012-02-15 15:43:06

回答

1

的问题是在这里:

private void initUi() { 
    LayoutInflater inflater = LayoutInflater.from(this); 

你没有但充气布局,因此,当你写

text[0] = (TextView) findViewById(R.id.text1); 

findview将返回null,因为没有任何视图搜索!

您必须首先膨胀布局,并且只有在您可以以编程方式更改其内容后。

+0

我没有明白,我该怎么办? – 2012-02-15 15:53:36

1

如果你得到一个NullPointerException调用TextView setText,然后你需要检查你的布局文件,以确保你的TextViews上有正确的ID。 findViewById返回一个空裁判,这意味着正在使用你传递的布局不存在的ID(R.layout.pages)

+0

对,我在R.layout.schedule中有这些id,我正在膨胀到R.layout.pages。我怎样才能引用R.layout.schedule的TextViews? – 2012-02-15 15:31:08

+0

您正在为layout.schedule充气后尝试从中解析您的TextView。你需要在你的循环之前膨胀“day”,然后调用day.findViewById。你现在正在做这件事的方式是,你在Activity的主布局上找到id为views的视图,这是layout.pages。 – Rich 2012-02-15 15:39:52

+0

刚刚更新了问题,发布了代码,但它仍然没有工作。真的不能做什么。 – 2012-02-15 15:54:15