gpt4 book ai didi

android - LogCat 不知所措,我不知道该怎么办...应用程序打开时关闭?

转载 作者:太空狗 更新时间:2023-10-29 16:22:09 25 4
gpt4 key购买 nike

这是 java 文件,唯一用过的...如果我知道这些错误意味着什么,我可以自己做

package tip.calculator;


import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;




public class TipCalculator extends Activity
{

private Button enter;
EditText myEditField = (EditText) findViewById(R.id.percentText);
final EditText myEditField2 = (EditText) findViewById(R.id.billText);

float percentage = 0;
float percentageInp = 0;
float billAmount = 0;
double output = 0;
String output1 = "";
Button clearButton = (Button) findViewById(R.id.clearButton);

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myEditField = (EditText) findViewById(R.id.percentText);
enter = (Button)findViewById(R.id.button1);


enter.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
TextView textView;
TextView errors;
textView = (TextView) findViewById(R.id.textView1);
errors = (TextView) findViewById(R.id.errorText);
for(;;){

if(myEditField == null){
break;
}

if(myEditField2 == null){
break;
}

}

percentageInp = Float.parseFloat(myEditField.getText().toString());
billAmount = Float.parseFloat(myEditField2.getText().toString());

percentage = ((float)percentageInp /100);

output = (double)(billAmount * percentage);

double result = output * 100;
result = Math.round(result);
result = result / 100;

output1 = Double.toString(result);

textView.setText(output1 + " $");

}
});

clearButton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

percentage = 0;
output = 0;
output1 = "";
TextView textView;

textView = (TextView) findViewById(R.id.textView1);

textView.setText("");

percentageInp = 0;
billAmount = 0;

myEditField.setText("");
myEditField2.setText("");


}


});
}
}

这是 logcat,我不知道这些错误是什么意思或如何处理它们...注意:这些仅在应用程序打开然后崩溃时产生

08-01 15:37:43.934: E/Trace(671): error opening trace file: No such file or directory (2)
08-01 15:37:44.704: E/AndroidRuntime(671): FATAL EXCEPTION: main
08-01 15:37:44.704: E/AndroidRuntime(671): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.TipCalculator}: java.lang.NullPointerException
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.os.Looper.loop(Looper.java:137)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:37:44.704: E/AndroidRuntime(671): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:37:44.704: E/AndroidRuntime(671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:37:44.704: E/AndroidRuntime(671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:37:44.704: E/AndroidRuntime(671): at dalvik.system.NativeStart.main(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671): Caused by: java.lang.NullPointerException
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.Activity.findViewById(Activity.java:1825)
08-01 15:37:44.704: E/AndroidRuntime(671): at tip.calculator.TipCalculator.<init>(TipCalculator.java:19)
08-01 15:37:44.704: E/AndroidRuntime(671): at java.lang.Class.newInstanceImpl(Native Method)
08-01 15:37:44.704: E/AndroidRuntime(671): at java.lang.Class.newInstance(Class.java:1319)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-01 15:37:44.704: E/AndroidRuntime(671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-01 15:37:44.704: E/AndroidRuntime(671): ... 11 more
08-01 15:38:44.993: E/Trace(741): error opening trace file: No such file or directory (2)
08-01 15:38:45.123: E/AndroidRuntime(741): FATAL EXCEPTION: main
08-01 15:38:45.123: E/AndroidRuntime(741): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.TipCalculator}: java.lang.NullPointerException
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.os.Looper.loop(Looper.java:137)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:38:45.123: E/AndroidRuntime(741): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:38:45.123: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:38:45.123: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:38:45.123: E/AndroidRuntime(741): at dalvik.system.NativeStart.main(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741): Caused by: java.lang.NullPointerException
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.Activity.findViewById(Activity.java:1825)
08-01 15:38:45.123: E/AndroidRuntime(741): at tip.calculator.TipCalculator.<init>(TipCalculator.java:19)
08-01 15:38:45.123: E/AndroidRuntime(741): at java.lang.Class.newInstanceImpl(Native Method)
08-01 15:38:45.123: E/AndroidRuntime(741): at java.lang.Class.newInstance(Class.java:1319)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-01 15:38:45.123: E/AndroidRuntime(741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-01 15:38:45.123: E/AndroidRuntime(741): ... 11 more

最佳答案

问题是您用来初始化 TipCalculator 类的成员字段的这些行:

EditText myEditField = (EditText) findViewById(R.id.percentText);
final EditText myEditField2 = (EditText) findViewById(R.id.billText);
Button clearButton = (Button) findViewById(R.id.clearButton);

在调用 setContentView 之前,您不能使用 findViewById。将上面的替换为:

EditText myEditField;
EditText myEditField2;
Button clearButton;

然后在调用 setContentView 之后将其添加到 onCreate 中的某处:

myEditField = (EditText) findViewById(R.id.percentText);
myEditField2 = (EditText) findViewById(R.id.billText);
clearButton = (Button) findViewById(R.id.clearButton);

附言解释 logcat 输出的方法是查找最后的“Caused by:”行并阅读该堆栈跟踪。之前的所有这些都与这种诊断任务无关。

关于android - LogCat 不知所措,我不知道该怎么办...应用程序打开时关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11768645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com