0

android/java/eclipse - i have this java file:(Eight.java)

/**
 * 
 */
package com.apptemplate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.hoerhager.christmas.R;

/**
     *
 */
public class Eight extends Activity {

    private  AdView adView;
    private Button btn81;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
         adView = (AdView)this.findViewById(R.id.adView);
         AdRequest adRequest = new AdRequest.Builder().build();
         adView.loadAd(adRequest);
  }



    private void initialiseUI()
    {
        btn81   = (Button)findViewById(R.id.button81);
        btn81.setOnClickListener((OnClickListener) this);
    }
     @Override
      public void onPause() {
        adView.pause();
        super.onPause();
      }

      @Override
      public void onResume() {
        super.onResume();
        adView.resume();
      }

      @Override
      public void onDestroy() {
        adView.destroy();
        super.onDestroy();
      }

        public void onClick(View v) 
        {
            if(v==btn81)
            {
                intent = new Intent(this,Eighta.class);
            }

            startActivity(intent);
        }

}

and this xml File: (eight.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/eight" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/ads_unit_id" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2.41"
        android:text="@string/text8" />

    <Button
        android:id="@+id/button81"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Preparation" />

</LinearLayout>

The button81 doesnt work, what is the error? I am new to work with Java. Its for a little android project for testing

6 Answers6

1

You missed to call your initialiseUI() function in onCreate() method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);
     adView = (AdView)this.findViewById(R.id.adView);
     AdRequest adRequest = new AdRequest.Builder().build();
     adView.loadAd(adRequest);
     initialiseUI();
}
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

You forgot to call you initialiseUI function in onCreate:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
         adView = (AdView)this.findViewById(R.id.adView);
         AdRequest adRequest = new AdRequest.Builder().build();
         adView.loadAd(adRequest);
         initialiseUI();
  }
Damien R.
  • 3,383
  • 1
  • 21
  • 32
0

Try this:

/**
 * 
 */
package com.apptemplate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.hoerhager.christmas.R;

/**
     *
 */
public class Eight extends Activity implements OnClickListener{

    private  AdView adView;
    private Button btn81;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
        adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        initialiseUI();
  }



    private void initialiseUI()
    {
        btn81   = (Button)findViewById(R.id.button81);
        btn81.setOnClickListener(this);
    }
     @Override
      public void onPause() {
        adView.pause();
        super.onPause();
      }

      @Override
      public void onResume() {
        super.onResume();
        adView.resume();
      }

      @Override
      public void onDestroy() {
        adView.destroy();
        super.onDestroy();
      }

        public void onClick(View v) 
        {
            if(v.getId()==R.id.button81)
            {
                intent = new Intent(this,Eighta.class);
            }

            startActivity(intent);
        }

}

CHANGES:

  • Now implements onclickListener
  • Call initialiseUI in "onCreate" method
  • initlialiseUI removed cast to onClickListener as it now implements onClickListener
  • onClick now uses the ID to test equality
Harry
  • 1,362
  • 12
  • 19
0

I think you forgot this implement, try as below:

public class Eight extends Activity implements OnClickListener {

Also your initialiseUI method it's not called. Try this instead:

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

    // call the method here
    initialiseUI();

}  

Finally, change the setOnClickListener like:

btn81.setOnClickListener(this);  
// this "attach" your button to the Activity which 
// implements the onClickListener method.  

And then, your OnClick method is wrong, see this answer, you have two ways to do it:

@Override
public void onClick(final View v) {
    if(v.getId() == R.id.button){ // do something }
}

// OR

@Override
public void onClick(final View v) {
    switch(v.getId()){
        case R.id.button:
              // do something
              break;
        case ...
    }
}

Hope this helps.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
0

Put this in oncreate()

 btn81   = (Button)findViewById(R.id.button81);
        btn81.setOnClickListener((OnClickListener) this);
keshav
  • 3,235
  • 1
  • 16
  • 22
0

you haven't called initialiseUI() in onCreate() and also implement the Click interface. That should do the trick.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74