1
화면이 항상 가로 모드 인 앱을 만들려고합니다.button.setonclick 리스너에서 null 포인터 예외가 발생합니다.
나는 이것을 구현하기 위해 setRequestedOrientation() 함수를 사용했다. 몇 가지 단추를 사용하여 계산기를 만들었습니다.
하지만 일부 기능을 에뮬레이터에서 실행하여 확인하려고 시도했을 때 Unfortunately, Your app has been stopped
오류가 발생했습니다.
기본적으로 버튼의 기능은 setOnClickListener
입니다. nullpointer exception
입니다. 나는 그것이 왜 그렇게인지 전혀 모른다. logcat 오류 :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john.mycalci/com.example.john.mycalci.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:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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.john.mycalci.MainActivity.onCreate(MainActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
이것은 내 Java 코드입니다.
TextView tvDisplay =(TextView)findViewById(R.id.tvDisplay);
Button bBack =(Button) findViewById(R.id.bBack);
final Button bRad =(Button) findViewById(R.id.bRad);
final Button bDegree =(Button) findViewById(R.id.bDegree);
Button bFact =(Button) findViewById(R.id.bFact);
Button bRp =(Button) findViewById(R.id.bRp);
Button bLp =(Button) findViewById(R.id.bLp);
Button bPercentage =(Button) findViewById(R.id.bPercentage);
Button bPower =(Button) findViewById(R.id.bPower);
Button bInv =(Button) findViewById(R.id.bInv);
final Button bSin =(Button) findViewById(R.id.bSin);
final Button bLn =(Button) findViewById(R.id.bLn);
Button bDivide =(Button) findViewById(R.id.bDivide);
Button bPi =(Button) findViewById(R.id.bPi);
final Button bCos=(Button) findViewById(R.id.bCos);
final Button bLog=(Button) findViewById(R.id.bLog);
Button bMultiply=(Button) findViewById(R.id.bMultiply);
Button bExp=(Button) findViewById(R.id.bExp);
final Button bTan=(Button) findViewById(R.id.bTan);
final Button bSqrt=(Button) findViewById(R.id.bSqrt);
Button bSubtract=(Button) findViewById(R.id.bSubtract);
Button bEqual=(Button) findViewById(R.id.bEqual);
Button bPower10=(Button) findViewById(R.id.bPowe10);
final Button bExpo=(Button) findViewById(R.id.bExpo);
Button bAns=(Button) findViewById(R.id.bAns);
Button bAdd=(Button) findViewById(R.id.Add);
Button bDot=(Button) findViewById(R.id.bDot);
Button bOne=(Button) findViewById(R.id.bOne);
Button bTwo=(Button) findViewById(R.id.bTwo);
Button bThree=(Button) findViewById(R.id.bThree);
Button bFour=(Button) findViewById(R.id.bFour);
Button bFive=(Button) findViewById(R.id.bFive);
Button bSix=(Button) findViewById(R.id.bSix);
Button bSeven=(Button) findViewById(R.id.bSeven);
Button bEight=(Button) findViewById(R.id.bEight);
Button bNine=(Button) findViewById(R.id.bNine);
bRad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = ((Button) v).getText().toString();
if(s=="Rad") {
bDegree.setText("Degree");
bRad.setText("");
}
}
});
bDegree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = ((Button) v).getText().toString();
if(s=="Rad") {
bDegree.setText("");
bRad.setText("Rad");
}
}
});
bInv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicks += 1;
if (clicks % 2 == 1) {
bCos.setText(Html.fromHtml("cos<sup>-1</sup>"));
bSin.setText(Html.fromHtml("sin<sup>-1</sup>"));
bTan.setText(Html.fromHtml("tan<sup>-1</sup>"));
bLn.setText(Html.fromHtml("e<sup>x</sup>"));
bLog.setText(Html.fromHtml("10<sup>x</sup>"));
bSqrt.setText(Html.fromHtml("x<sup>2</sup>"));
bExpo.setText("root");
}
else{
bCos.setText("cos");
bSin.setText("sin");
bTan.setText("tan");
bLn.setText("ln");
bLog.setText("log");
bSqrt.setText("sqrt");
bExpo.setText("^");
}
}
});
}
layout-land
리소스를 사용하려고합니다.
가능한 복제 [NullPointerException이 무엇인가, 나는 그것을 해결 어떻게?] (http://stackoverflow.com/questions/218384/what-is-a를 -nullpointereception-and-how-do-i-fix-it) – Turing85
모든 ID가 두 레이아웃에서 모두 사용 가능한지 확인하십시오. – FlanschiFox
앱을 충돌시킨 버튼은 무엇입니까? –