我是java的新手,所以如果有人能够解释它就会很棒。这也是在Android工作室顺便说一下。每当我尝试这样做时,我都会收到错误。
int next = 0;
public class Outlaw {
String name;
String message;
public Outlaw(String name, String message){
}
public String getName(){
return name;
}
public String getMessage(){
return message;
}
}
Outlaw outlaw0;
Outlaw outlaw1;
Outlaw outlaw2;
Outlaw outlaw3;
Outlaw outlaw4;
Outlaw outlaw5;
Outlaw outlaw6;
Outlaw outlaw7;
ArrayList<Outlaw> outlaws;
public void continueFunct(View view){
Log.i("outlaws",outlaws.get(0).getMessage());
if (step == 0){
step =+ 1;
infoTextView.setText(outlaws.get(0).getMessage());
} else if (step == 1){
show();
next =+ 1;
}
}
然后我在onCreate方法中有这个
outlaws = new ArrayList<>();
outlaw0 = new Outlaw("Trader Joe", "You caught Trader Joe selling faulty Lemons!");
outlaw1 = new Outlaw("Dirty Dan", "\"No I'm Dirty Dan!\"");
outlaw2 = new Outlaw("Itchy Trigger", "Well, Well, Well, if it isn't Itchy Trigger");
outlaw3 = new Outlaw("Eye-Twitch Bill", "Agh! A monster! Oh never mind it's just Eye-Twitch Bill");
outlaw4 = new Outlaw("Skinny Pete", "Sooo we meet again Skinny Pete");
outlaw5 = new Outlaw("Whiskey Larry", "A drunken whiskey Larry stumbles into view");
outlaw6 = new Outlaw("Alamo Jack", "A new challenger approaches! Alamo Jack!");
outlaw7 = new Outlaw("El Loco", "\"That was my amigo! You will pay for what you have done!\"");
outlaws.add(outlaw0);
outlaws.add(outlaw1);
outlaws.add(outlaw2);
outlaws.add(outlaw3);
outlaws.add(outlaw4);
outlaws.add(outlaw5);
outlaws.add(outlaw6);
outlaws.add(outlaw7);
我知道我已经导入了所有的编写框架,但每次单击“继续”按钮时我都会收到错误。当我摆脱日志语句时,我试图设置的textView只是空白,使用Log语句应用程序崩溃。
这是我收到的错误消息,不知道该如何处理它可能会有所帮助。
05-26 20:44:08.826 21384-21384/com.scottolsonofficial.westventure E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.scottolsonofficial.westventure, PID: 21384
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.i(Log.java:164)
at com.scottolsonofficial.westventure.MainActivity.continueFunct(MainActivity.java:106)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:2)
您似乎忘记了分配班级的字段。
您的构造函数当前不执行任何操作,因此getMessage()
会返回null
,因为它从未被分配过。
public class Outlaw {
private String name;
private String message;
public Outlaw(String name, String message){
this.name = name;
this.message = message;
}
public String getName(){
return name;
}
public String getMessage(){
return message;
}
}
答案 1 :(得分:-1)
您的getMessage函数返回一个null对象。
您的代码的另一部分似乎是修改消息。我会将消息声明为
final String message;
这将确保代码中的otger部分无法使用它