• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps I keep getting errors and i dont know why.HELP

Okay im doing simple stuff and i dont know why im getting errors can you guys please help me out.I get errors on this line "add = (Button) findviewById(R.id.Btnadd);" and "counter".

Java code
PHP:
package com.example.counter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;

public class Main extends Activity {

    int counter;
    Button add,sub;
    TextView Display;

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

    counter = 0;
    add = (Button) findviewById(R.id.Btnadd);
    

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

}
PHP:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

  >

    <TextView
  android:id="@+id/Tv1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="45sp"
  android:layout_gravity="center"
  android:text="@string/TvDisplay"
  android:textColor="#5DA2FC" />

    <Button

  android:id="@+id/Btnadd"
  android:layout_width="250dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:text="@string/Btn1"
  />

    <Button

  android:id="@+id/Btnsub"
  android:layout_width="250dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:text="@string/Btn2"
  />

</LinearLayout>
 
Move the initialisation code into the onCreate function.

Code:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter = 0;
        add = (Button) findviewById(R.id.Btnadd);
        }
Omg im such an idiot.Thanks so much.Here i was trying to figure it out and it was something so simple.Well i will be sure never to make the same mistake again.Thank you again.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones