它可能是问题的java文件,它根本与xml无关:
package com.example.android.randomnumber;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText from = (findViewById(R.id.editTextFrom));
EditText to = (findViewById(R.id.editTextTo));
TextView output =(findViewById(R.id.textViewOutput));
String wfrom = from.getText().toString();
int numberfrom=Integer.parseInt(wfrom);
String wto = to.getText().toString();
int numberto=Integer.parseInt(wto);
Random randomNumberGenerator =new Random();
int number = randomNumberGenerator.nextInt(numberto-numberfrom)+numberfrom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output.setText(number);
}
public void generate(View view){
if (from.getText() == null || "".equals(from.getText().toString())){
Toast.makeText(MainActivity.this, "the -From- value is empty!",
Toast.LENGTH_SHORT).show();
return;
}else if (to.getText() == null || "".equals(to.getText().toString())){
Toast.makeText(MainActivity.this, "the -To- value is empty!",
Toast.LENGTH_SHORT).show();
return;
}else if(numberfrom >= numberto){
Toast.makeText(MainActivity.this, "the -To- value must be greater than the -From- value",
Toast.LENGTH_SHORT).show();
return;
}
Log.d("test", "it's from:"+numberfrom+" to this:"+numberto+"generatednumber"+number);
}
}
Logcat中的错误提前谢谢:
02-24 14:16:33.865 23117-23117/com.example.android.randomnumber E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.randomnumber, PID: 23117
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.randomnumber/com.example.android.randomnumber.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2989)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.android.randomnumber.MainActivity.<init>(MainActivity.java:15)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1690)
at android.app.Instrumentation.newActivity(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2979)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
我试图通过我的手机运行它问题是stil和问题开始toshow当我添加output.setText并再次感谢你...................... .................................................. ...
您的逻辑代码不正确,请将其更改为
public class MainActivity extends AppCompatActivity {
EditText from;
EditText to;
TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
from = (findViewById(R.id.editTextFrom));
to = (findViewById(R.id.editTextTo));
output = (findViewById(R.id.textViewOutput));
}
public void generate(View view) {
// Get values of from and to edit text
String wfrom = from.getText().toString();
String wto = to.getText().toString();
// Validate from value
if (TextUtils.isEmpty(wfrom)) {
Toast.makeText(MainActivity.this, "the -From- value is empty!",
Toast.LENGTH_SHORT).show();
return;
}
// Validate to value
if (TextUtils.isEmpty(wto)) {
Toast.makeText(MainActivity.this, "the -To- value is empty!",
Toast.LENGTH_SHORT).show();
return;
}
int numberfrom = Integer.parseInt(wfrom);
int numberto = Integer.parseInt(wto);
// Check whether from larger or equal than to
if (numberfrom >= numberto) {
Toast.makeText(MainActivity.this, "the -To- value must be greater than the -From- value",
Toast.LENGTH_SHORT).show();
return;
}
// Generate a random number in range [from - to]
Random randomNumberGenerator = new Random();
int number = randomNumberGenerator.nextInt(numberto - numberfrom) + numberfrom;
// Display the random number on output text view
output.setText(number + "");
// Log for debug
Log.d("test", "it's from:" + numberfrom + " to this:" + numberto + "generatednumber" + number);
}
}