当我在img1
中声明img2
和onCreate()
变量时,我的问题已解决。但是,我确实需要全局声明这些变量。有什么想法吗?
package com.example.ui;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/*img1 and img2 veriables below*/
ImageView img1 = findViewById(R.id.img1);
ImageView img2 = findViewById(R.id.img2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*my problem is fixed when I declare those variables here, but I need them to be global*/
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch(id){
case R.id.img1:
Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
break;
case R.id.img2:
Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
在将视图附加到班级之前就已经找到了视图
这是正确的方法:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/*img1 and img2 veriables below*/
ImageView img1;
ImageView img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = findViewById(R.id.img1);
img2 = findViewById(R.id.img2);
/*my problem is fixed when I declare those variables here, but I need them to be global*/
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch(id){
case R.id.img1:
Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
break;
case R.id.img2:
Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:1)
修改您的
ImageView img1;//only declaration
ImageView img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img1 = findViewById(R.id.img1);
ImageView img2 = findViewById(R.id.img2);
/*my problem is fixed when I declare those variables here, but I need them to be global*/
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
答案 2 :(得分:1)
您不能在那里使用findViewById,请尝试以下操作:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
private ImageView img1;
private ImageView img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = findViewById(R.id.img1);
img2 = findViewById(R.id.img2);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch(id){
case R.id.img1:
Toast.makeText(this, "img1", Toast.LENGTH_SHORT).show();
break;
case R.id.img2:
Toast.makeText(this, "img2", Toast.LENGTH_SHORT).show();
}
}
答案 3 :(得分:1)
我真的无法理解您的意思。您是要让img1和img2在活动中还是在应用程序中变得全球化?
实际上,我想您会误解该程序的顺序。在onCreate()方法之后,电话窗口将被初始化。然后,setContentView()方法将视图绑定到电话窗口中。因此,应该在setContentView()之后在onCreate()方法中绑定imageView。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/*img1 and img2 veriables below*/
ImageView img1;
ImageView img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = findViewById(R.id.img1);
img2 = findViewById(R.id.img2);
/*my problem is fixed when I declare those variables here, but I need them to be global*/
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
}
答案 4 :(得分:0)
您应该有以下几行:
ImageView img1 = findViewById(R.id.img1);
ImageView img2 = findViewById(R.id.img2);
在“ setContentView(R.layout.activity_main);”之后的OnCreate方法内部