Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android textview - i cannot get it going

Hi Iam trying to build my first android project. I am quiete a good programmer, for microcontrollers, php, bash etc.

However this java syntax is making want to explode.

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv1;


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText("Text2");

}

and

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:editable = "true" />

I checked many examples on the web but it looks like i cannot get it going.

tv1.setText("Text2"); always comes up as syntax error.

Where am I making a mistake here?

like image 423
Sebastian Heyn Avatar asked Feb 15 '23 11:02

Sebastian Heyn


1 Answers

Change to

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // initialize after setContentView
    tv1 = (TextView) findViewById(R.id.textView1);
    tv1.setText("Text2"); // should be within a method
}
like image 138
Raghunandan Avatar answered Feb 17 '23 01:02

Raghunandan