John
John

Reputation: 1

Can't resolve symbol "context" on simple program

I'm trying to create a simple hello world program to get my started on Android app development, but I keep getting an error on the TextView line with context on it with "Cannot resolve symbol "Context"

package com.example.bryce.firstapplication;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onResume() {
    super.onResume();
    Context context=this;
    TextView TV=new TextView(context: this);
    TV.setText("Hello World");
    setContentView(TV);
    //setContentView(R.layout.activity_main);
}
}

Upvotes: 0

Views: 1848

Answers (5)

Terence
Terence

Reputation: 1

The context isn't necessary. You can display the text you want with this:

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

    TextView textView = findViewById(R.id.textview);
    textView.setText("Hello World");
}

Just make sure your TextView has the an id like:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Upvotes: 0

Saeed
Saeed

Reputation: 3520

Change TextView TV=new TextView(context:this) line to :

TextView TV=new TextView(this);

or

TextView TV=new TextView(MainActivity.this);

or

Context context=this;
TextView TV=new TextView(context);

Another way is use this code in your activity class:

TextView TV=new TextView(getBaseContext());

Upvotes: 0

Faiizii Awan
Faiizii Awan

Reputation: 1710

Replace

TextView TV=new TextView(context: this);

with this

TextView TV=new TextView(MainActivity.this);

Upvotes: 0

Momen Zaqout
Momen Zaqout

Reputation: 1498

I think you may put your code inside onCreate instead of onResume

anyway, just replace

TextView TV=new TextView(context: this);

with

TextView TV=new TextView(MainActivity.this);

so you can use your activity to declare the TextView

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006554

Replace:

TextView TV=new TextView(context: this);

with:

TextView TV=new TextView(this);

Also note that the code that you have in onResume() normally goes in onCreate().

Upvotes: 3

Related Questions