Reputation: 1
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
Upvotes: 0
Views: 118
Reputation: 11988
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.
Upvotes: 0
Reputation: 1472
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:
Upvotes: 0
Reputation: 5063
you haven't called initialiseUI()
in onCreate()
and also implement the Click interface
. That should do the trick.
Upvotes: 0
Reputation: 3255
Put this in oncreate()
btn81 = (Button)findViewById(R.id.button81);
btn81.setOnClickListener((OnClickListener) this);
Upvotes: 0
Reputation: 18923
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();
}
Upvotes: 1
Reputation: 3373
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();
}
Upvotes: 0