如何使用 类 和实例将变量传递给 findViewById 方法?
How to pass a variable to findViewById method using classes and instances?
我最近一直在尝试在android工作室制作这个井字游戏,我对android工作室和java只有基本的了解,所以我想通过制作 类 和对象来完成这个应用程序,但是每当我尝试 运行 我的应用程序时,应用程序崩溃了,我不知道它正确编译的代码有什么问题,有人可以帮我吗?
这是 logcat window:
11-04 23:48:20.249 4534-4534/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-04 23:48:20.258 4534-4534/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-04 23:48:20.480 4534-4534/com.example.home.tictactoe I/InstantRun: starting instant run server: is main process
11-04 23:48:20.845 4534-4534/com.example.home.tictactoe D/AndroidRuntime: Shutting down VM
11-04 23:48:20.851 4534-4534/com.example.home.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.home.tictactoe, PID: 4534
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.tictactoe/com.example.home.tictactoe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at com.example.home.tictactoe.MainActivitypawn.hideObj(MainActivity.java:45)
at com.example.home.tictactoe.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的 MainActivity.java 文件
package com.example.home.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public void clickButton(View view){
Log.i("test","working");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class pawn{
int resID = 0;
ImageView object2 = (ImageView) findViewById(resID);
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
final pawn x_11 = new pawn();
final pawn zero_11 = new pawn();
x_11.resID = R.id.x11;
zero_11.resID=R.id.zero11;
x_11.hideObj();
}
}
见图:
ImageView object2 = (ImageView) findViewById(resID);
在创建 pawn
对象时被调用。此时您还没有为您的对象设置 resId,因此它将为 0。这导致 object2
到 null
。
您可以这样更改 class:
class pawn {
int resID = 0;
ImageView object2;
public pawn(int resID) {
this.resID = resID;
object2 = (ImageView) findViewById(resID)
}
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
那就用吧
pawn x_11 = new pawn(R.id.x11);
x_11.hideObj();
我最近一直在尝试在android工作室制作这个井字游戏,我对android工作室和java只有基本的了解,所以我想通过制作 类 和对象来完成这个应用程序,但是每当我尝试 运行 我的应用程序时,应用程序崩溃了,我不知道它正确编译的代码有什么问题,有人可以帮我吗?
这是 logcat window:
11-04 23:48:20.249 4534-4534/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-04 23:48:20.258 4534-4534/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-04 23:48:20.480 4534-4534/com.example.home.tictactoe I/InstantRun: starting instant run server: is main process
11-04 23:48:20.845 4534-4534/com.example.home.tictactoe D/AndroidRuntime: Shutting down VM
11-04 23:48:20.851 4534-4534/com.example.home.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.home.tictactoe, PID: 4534
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.tictactoe/com.example.home.tictactoe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at com.example.home.tictactoe.MainActivitypawn.hideObj(MainActivity.java:45)
at com.example.home.tictactoe.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的 MainActivity.java 文件
package com.example.home.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public void clickButton(View view){
Log.i("test","working");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
class pawn{
int resID = 0;
ImageView object2 = (ImageView) findViewById(resID);
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
final pawn x_11 = new pawn();
final pawn zero_11 = new pawn();
x_11.resID = R.id.x11;
zero_11.resID=R.id.zero11;
x_11.hideObj();
}
}
见图:
ImageView object2 = (ImageView) findViewById(resID);
在创建 pawn
对象时被调用。此时您还没有为您的对象设置 resId,因此它将为 0。这导致 object2
到 null
。
您可以这样更改 class:
class pawn {
int resID = 0;
ImageView object2;
public pawn(int resID) {
this.resID = resID;
object2 = (ImageView) findViewById(resID)
}
public void showObj()
{
object2.animate().alpha(1f).setDuration(1000);//add code here to change alpha of object
}
public void hideObj()
{
object2.animate().alpha(0f).setDuration(1000);
}
}
那就用吧
pawn x_11 = new pawn(R.id.x11);
x_11.hideObj();