按钮不起作用java android

时间:2013-12-20 13:52:38

标签: java android

android / java / eclipse - 我有这个java文件:(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);
        }

}

和这个xml文件:(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>

button81不起作用,错误是什么?我是Java的新手。它用于测试的小型android项目

6 个答案:

答案 0 :(得分:1)

您错过了在 onCreate() 方法中调用 initialiseUI() 函数:

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();
}

答案 1 :(得分:0)

您忘记在initialiseUI中致电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();
  }

答案 2 :(得分:0)

试试这个:

/**
 * 
 */
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);
        }

}

<强>变化:

  • 现在实现onclickListener
  • 在“onCreate”方法中调用initialiseUI
  • initlialiseUI已将强制转换为onClickListener,因为它现在实现了onClickListener
  • onClick现在使用ID来测试相等性

答案 3 :(得分:0)

我想你忘了这个工具,试试如下:

  

public class Eight extends Activity implements OnClickListener {

您的 initialiseUI 方法也未被调用。试试这个:

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

    // call the method here
    initialiseUI();

}  

最后,更改setOnClickListener,如:

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

然后,您的 OnClick 方法有误,请参阅this answer,您有两种方法可以执行此操作:

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

希望这有帮助。

答案 4 :(得分:0)

将它放在oncreate()

 btn81   = (Button)findViewById(R.id.button81);
        btn81.setOnClickListener((OnClickListener) this);

答案 5 :(得分:0)

您尚未在initialiseUI()中致电onCreate()并实施Click interface。这应该可以解决问题。