活动已停止

时间:2018-11-22 07:56:22

标签: java android android-studio android-layout android-activity

这是代码 当我应用该代码应用程序已停止时 这段代码是在android应用程序开发中找到相对素数 检查并让我知道我的错误 预先感谢

relative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String A = num1.getText().toString();
String B = num2.getText().toString();
int val1 = Integer.parseInt(A);
int val2 = Integer.parseInt(B);
int gcd = 1;
// Function to find GCD for relative prime
while (val1 != val2) {
if (val1 > val2)
val1 = val1 - val2;
else
val2 = val2 - val1;
}
String ans = String.valueOf(val1);
String ans1 = String.valueOf(val2);
// function to check and print if
// two numbers are co-prime or not
if (ans1.equals(1)) {
result.setText("Relative Prime");
Toast.makeText(MainActivity.this, "Relative Prime", 
Toast.LENGTH_SHORT).show();
} else {
result.setText("Not Relative Prime");
Toast.makeText(MainActivity.this, "Not Relative Prime", 
Toast.LENGTH_SHORT).show();
}
}
});

错误日志

11-22 13:00:21.854 6630-6630/com.example.jasirali.layout_elements E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jasirali.layout_elements, PID: 6630
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jasirali.layout_elements/com.example.jasirali.layout_elements.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.jasirali.layout_elements.MainActivity.onCreate(MainActivity.java:128)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
        at android.app.ActivityThread.access$800(ActivityThread.java:144) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5221) 
        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:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

1 个答案:

答案 0 :(得分:0)

relative变量是null。检查是否通过findViewById()

获得

这应该可以解决问题。