2016-11-09 3 views
-1

テキストビューに何も入力しません my code;ViewPagerでテキストを設定できません

public class MainActivity extendsアクティビティ{ プライベートボタンボタン。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    button=(Button)findViewById(R.id.button); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Intent intent = new Intent(MainActivity.this,page.class); 
      startActivity(intent); 

     } 
    }); 

} 

}

public class CustomPagerAdapter extends PagerAdapter { 

private Context mContext; 

public CustomPagerAdapter(Context context) { 
    mContext = context; 
} 

@Override 
public Object instantiateItem(ViewGroup collection, int position) { 
    ModelObject modelObject = ModelObject.values()[position]; 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false); 
    collection.addView(layout); 
    return layout; 
} 

@Override 
public void destroyItem(ViewGroup collection, int position, Object view) { 
    collection.removeView((View) view); 
} 

@Override 
public int getCount() { 
    return ModelObject.values().length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == object; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    ModelObject customPagerEnum = ModelObject.values()[position]; 

    return mContext.getString(customPagerEnum.getTitleResId()); 
} 

}

public enum ModelObject { 

WHİTE(R.string.white,R.layout.view_white), 
GREEN(R.string.green, R.layout.view_green); 

private int mTitleResId; 
private int mLayoutResId; 

ModelObject(int titleResId, int layoutResId) { 
    mTitleResId = titleResId; 
    mLayoutResId = layoutResId; 
} 

public int getTitleResId() { 
    return mTitleResId; 
} 

public int getLayoutResId() { 
    return mLayoutResId; 
} 

}

public class page extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_white); 

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
    viewPager.setAdapter(new CustomPagerAdapter(this)); 




} 

}

パブリッククラスG reenPage extends Activity { プライベートTextViewテキスト。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_green); 

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); 
    viewPager.setAdapter(new CustomPagerAdapter(this)); 

    text=(TextView)findViewById(R.id.textView2); 

    /*not working ->*/ text.setText("HELLO"); 

} 

}

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true"> 
    <TextView 
     android:id="@+id/textview1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="30sp" 
     android:textStyle="normal" 
     /> 



</android.support.v4.view.ViewPager> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:background="@android:color/holo_red_dark" 
      android:layout_height="match_parent"> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="193dp" 
    android:id="@+id/textView2"/> 

私を助けてください。 私はあまりよく分かりません。 あなたはわかりやすいでしょうか?

答えて

0

文字列を使用してみてください、あなたがにのTextViewをキャストtextView2のIDがごactivity.xmlで実際に

であることを確認してください:

final String HELLO = "HELLO"; 

TextView text = (TextView)findViewById(R.id.textView2); 
text.setText("" + HELLO); 

代替textView2で現在利用可能なテキストがない場合には重要なことをヒントにしてからオンに変更するテキストを変更する

この方法でもプロジェクトを再構築しようとしないと、コードをどこかにコピーしたことが原因である可能性があります。プロジェct。

関連する問題