尝试将文字设置为textview时,获取textview=null
我不知道为什么。请注意,我正在从另一个班级拨打hesham2010
。
public class StandaloneExample extends Activity {
TextView textViewContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.standalone_example);
textViewContent = (TextView) this.findViewById(R.id.textView); // working
textViewContent.setText("test"); // working fine
}
public void hesham2010(String titile) {
// here textview null why?
textViewContent.setText(titile);
}
其他课程:
public class PageCurlView extends View {
private void nextView() {
MySQLiteHelper SqlLiteInstance = new MySQLiteHelper(hesham);
SqlLiteInstance.insertForTest("تايتل","لبلب","ثثث");
SqlLiteInstance.insertForTest("تايتل التاني","5555","5555");
SqlLiteInstance.insertForTest("التالت","66666","66666");
int foreIndex = mIndex + 1;
Cursor myDataBase= SqlLiteInstance.getCurrentPageData(1);
StandaloneExample hesham55 = new StandaloneExample();
if(myDataBase.moveToFirst() && myDataBase.getCount() >= 1){
do{
hesham55.hesham2010(myDataBase.getString(0));
}while(myDataBase.moveToNext());
}
xml文件:
<RelativeLayout
android:id="@+id/game_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.mystictreegames.pagecurl.PageCurlView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/dcgpagecurlPageCurlView1"
android:background="@drawable/facebook">
</com.mystictreegames.pagecurl.PageCurlView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:drawableRight="@drawable/up"
android:drawableLeft="@drawable/down"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="جوهر كون المرء انه انسان لا يسعى الى الكمال"/>
答案 0 :(得分:1)
onCreate()
方法是Activity Life cycle方法之一。
如果您在其他课程中使用它,除非已创建Activity
,否则您将无法使用它。并且您不应该从另一个Activity
更改Activity
{1}} ..如果您想在活动之间传递信息,可以使用Intent
s。
你有答案:
How to pass an object from one activity to another on Android
希望有所帮助