1

I would like to ask why I'm getting an error on setting the text of TextView. I'm viewing here the beginner layout, but the textview is in my gameplay layout. Is that possible? Here is my code:

public class Beginner extends Activity 
{

    Button beginner1, beginner2;
    TextView category;

    @Override
    protected void onCreate(Bundle MainPage) 
    {
        super.onCreate(MainPage);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.beginner);

        beginner1 = (Button) findViewById(R.id.btnBeginner1);
        beginner2 = (Button) findViewById(R.id.btnBeginner2);

        beginner1.setOnClickListener(myOnlyhandler);
        beginner2.setOnClickListener(myOnlyhandler);

        category = (TextView) findViewById(R.id.tvCategory);

    }

    View.OnClickListener myOnlyhandler = new View.OnClickListener(){

    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch(v.getId()) {
        case R.id.btnBeginner1:
            Intent openBeginner = new Intent("com.thesis.logipic.GAMEPLAY");
            startActivity(openBeginner);
            category.setText("Category");
        break;
        case R.id.btnBeginner2:
            Intent openBeginner2 = new Intent("com.thesis.logipic.GAMEPLAY");
            startActivity(openBeginner2);  
            category.setText("Category");
        break;
        }
    }
    };

}

And this is the error from my Logcat:

10-08 21:26:30.486: D/AndroidRuntime(1019): Shutting down VM
10-08 21:26:30.486: W/dalvikvm(1019): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-08 21:26:30.506: E/AndroidRuntime(1019): FATAL EXCEPTION: main
10-08 21:26:30.506: E/AndroidRuntime(1019): java.lang.NullPointerException
10-08 21:26:30.506: E/AndroidRuntime(1019):     at com.thesis.logipic.Beginner$1.onClick(Beginner.java:42)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.view.View.performClick(View.java:2408)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.view.View$PerformClick.run(View.java:8816)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.os.Handler.handleCallback(Handler.java:587)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.os.Looper.loop(Looper.java:123)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at java.lang.reflect.Method.invoke(Method.java:521)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-08 21:26:30.506: E/AndroidRuntime(1019):     at dalvik.system.NativeStart.main(Native Method)
10-08 21:26:32.466: I/Process(1019): Sending signal. PID: 1019 SIG: 9
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Aldy
  • 83
  • 1
  • 10

5 Answers5

0

put category.setText("Category") before startActivity()

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • tried it even w/out the Intent openBeginner = new Intent("com.thesis.logipic.GAMEPLAY"); startActivity(openBeginner); still got the same error – Aldy Oct 08 '13 at 13:35
0

this not possible to access to other resource that you didn't initialize it in setContentView. tell me why you didn't use it in the same Layout?

Majid Daeinejad
  • 1,037
  • 8
  • 19
  • because it should just change the text of the game in another layout its like this is the category and the other layout is where the question is something like that – Aldy Oct 08 '13 at 13:43
0

As you said its in a different layout. In order to access it you have to inflate it first.

Or you could just move your TextView into the R.layout.beginner layout.

kupsef
  • 3,357
  • 1
  • 21
  • 31
0

Your TextView has to be in the layout that you set in onCreate() using setContentView(). You get NullPointerException because you try to find view which is not part of you layout.

If you want to add view to your layout, you can do this programmatically. You have to create your view programmatically and then add it to your layout using yourLayout.addView(textView, params).

You have two options to crete view:

  1. Create the view using its constructor and methods.
  2. Inflate view using LayoutInflater. See this answer. But if you want to do this, your TextView has to be in new layout (category_layout.xml) and it has to be the top level view (you don't have to place it in other layout).
Community
  • 1
  • 1
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
0

You are doing a reference through ID of a TextView "tvCategory" that is NOT in the beginner layout that you were inflated. Why you don't do this once the second activity is loaded? or by default inside the XML?

Mr.Moustard
  • 1,297
  • 21
  • 24