-3

When try to set text to textview it get textview=null I don't know why. Please note that I'm calling hesham2010 from another class.

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);
    }

other class :

 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 file :

  <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="جوهر كون المرء انه انسان لا يسعى الى الكمال"/>

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Is onCreate called before hesham2010 is called? – Jens May 24 '14 at 22:23
  • post the `activity's code` where u call `hesham2010(String titile)` – Kaushik May 24 '14 at 22:23
  • yes it's called and test is shown in activity but when calling hesham2010 from other class i get null – user3670735 May 24 '14 at 22:24
  • done see the calling class and xml code – user3670735 May 24 '14 at 22:36
  • Quite simply...you can NOT do this `StandaloneExample hesham55 = new StandaloneExample();`. It is not possible to create an instance of an `Activity` using `new`. You should never create `public` variables or methods in an `Activity` with the purpose of accessing them from any other class - that's not how an `Activity` is designed to work. – Squonk May 24 '14 at 23:20

1 Answers1

1

The onCreate() method is one of the Activity Life cycle methods.

If you are using it with another class you won't be able to use it unless the Activity has been created.. And you shouldn't change Activity from another Activity.. If you want to pass information between activities you can use Intents.

You have the answer here:

How to pass an object from one activity to another on Android

Hope that helps

Community
  • 1
  • 1
Aviad
  • 1,539
  • 1
  • 9
  • 24
  • the other class is custom view , StandaloneExample hesham55 = new StandaloneExample(); hesham55.hesham2010(myDataBase.getString(0)); – user3670735 May 24 '14 at 22:34
  • You shouldn't put Activity inside a view container.. You should do the following structure: Activity Service layer(Regular Java class that hold the myDataBase); Activtity should call the method when loading the db.. Not the opposite – Aviad May 24 '14 at 22:38
  • I'm not put activity inside view container , view container has string and i want to send this string to textview in activity , but textview get null – user3670735 May 24 '14 at 22:42
  • 1
    You are doing it wrong.. This not should be like that. Android OS is creating the activity obejct.. Look at this link it has full tutorial how to use android and sqlHelper.. http://hmkcode.com/android-simple-sqlite-database-tutorial/ You sholud do the same structure – Aviad May 24 '14 at 22:49
  • I'm not talking about using sqllite or getting values from it , it works fine with me , let say i'm not using sql lite :) , for now i want to call method in activity from the pageCurlView , why textView give null ??! the problem in the textView not with sql database or the starc within in it – user3670735 May 24 '14 at 22:52
  • Beacuse you cannot do it in your way.. View doesn't call activity. It should be the opposite.. View cannot call activity.. When activity is created by android OS it will create the xml.. So the problem with your class structure and that why i sent you the link.. You don't need this custom view.. If you follow the tutorial even without the sql implementation you will see it will work.. – Aviad May 24 '14 at 22:55
  • ok i can't remove that class cuz it huge it for android curl effect , but now i want to use this textView inside the pagecurle class , how i can do this ? – user3670735 May 24 '14 at 22:57
  • You need to create the text view inside the custom view.. I attached a simple link that show what you need http://stackoverflow.com/questions/10781218/android-custom-view-textviewbuttonsome-customized-behaviors – Aviad May 24 '14 at 23:01
  • thankx for the link , but this not working for me , it telling me The method inflate(int, ViewGroup, boolean) in the type LayoutInflater is not applicable for the arguments (Context, int, PageCurlView) – user3670735 May 24 '14 at 23:36
  • You can use this line instead.. Try it. Not sure why this not working.. it should work fine LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); You need to start from the basics because you won't be able to create application with the current approach – Aviad May 25 '14 at 11:04