Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我在andiroid studio中的第二个按钮不起作用,甚至我在特定按钮的onclick属性中设置了不同的方法_Android - Fatal编程技术网

Android 我在andiroid studio中的第二个按钮不起作用,甚至我在特定按钮的onclick属性中设置了不同的方法

Android 我在andiroid studio中的第二个按钮不起作用,甚至我在特定按钮的onclick属性中设置了不同的方法,android,Android,这是我的主要活动计划: import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private static Ima

这是我的主要活动计划:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
 private static ImageView im1;
 private static Button btm,btm2;
private int current_image_index;
int[] images={R.mipmap.mylogo,R.mipmap.picasa,R.mipmap.twitter};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onclk(View v)
{
  im1=(ImageView)findViewById(R.id.imageView);
    btm=(Button)findViewById(R.id.button);

    btm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index++;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
public void onclk2(View v)
{
    im1=(ImageView)findViewById(R.id.imageView);
    btm2=(Button)findViewById(R.id.button2);

    btm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index--;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
}
我设计了两个名为forwardbutton和BackwardButton的按钮,我为每个按钮设置了onclick属性,第一个是btton onclik,第二个是button onclk2函数。。。。。
但是,在运行应用程序后退按钮时,此按钮不起作用

请将此代码替换为您的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
 private static ImageView im1;
 private static Button btm,btm2;
private int current_image_index;
int[] images={R.mipmap.mylogo,R.mipmap.picasa,R.mipmap.twitter};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onclk(View v)
{
  im1=(ImageView)findViewById(R.id.imageView);
    btm=(Button)findViewById(R.id.button);

    btm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index++;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
public void onclk2(View v)
{
    im1=(ImageView)findViewById(R.id.imageView);
    btm2=(Button)findViewById(R.id.button2);

    // Your Error Was Here/in Below Line
    btm2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index--;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
}
public void onclk2(View v)
{
    im1=(ImageView)findViewById(R.id.imageView);
    btm2=(Button)findViewById(R.id.button2);

    btm2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index--;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
希望这将有助于……:

使用

btm2.setOnClickListener
代替

btm.setOnClickListener

在第二个函数中

完整的代码是

    public void onclk2(View v)
{
    im1=(ImageView)findViewById(R.id.imageView);
    btm2=(Button)findViewById(R.id.button2);


    btm2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            current_image_index--;
            current_image_index = current_image_index % (images.length);
            im1.setImageResource(images[current_image_index]);
        }
    });
}
}

在您的代码中..您使用相同的名称调用clicklistner。请将其替换为btm2.setonClickListner您应该将示例减少到最低限度,以便更容易理解。