每当我尝试在模拟器上运行以下计算器应用程序时,它立即崩溃。不知道Java,阅读logcat有点压倒性。
该计划的目的是创建一个使用无线电组的体重转换应用程序,并将千克数转换为磅数,反之亦然。
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.androidbootcamp.medicalcalculator"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我的logcat:
>02-26 11:14:42.147 1535-1535/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.androidbootcamp.medicalcalculator/net.androidbootcamp.medicalcalculator.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at net.androidbootcamp.medicalcalculator.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是我的MainActivity:
package net.androidbootcamp.medicalcalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
double conversionRate = 2.2;
double weightEntered;
double convertedWeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
final EditText weight = (EditText)findViewById(R.id.txtWeight);
final RadioButton lbToKilo = (RadioButton)findViewById(R.id.radLbToKilo);
final RadioButton kiloToLb = (RadioButton)findViewById(R.id.radKiloToLb);
final TextView result = (TextView)findViewById(R.id.txtResult);
Button convert = (Button)findViewById(R.id.btnConvert);
convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
weightEntered=Double.parseDouble(weight.getText().toString());
DecimalFormat tenth = new DecimalFormat("#.#");
if(lbToKilo.isChecked()){
if (weightEntered <= 500){
convertedWeight = weightEntered / conversionRate;
result.setText(tenth.format(convertedWeight) + "kilograms");
} else {
Toast.makeText(MainActivity.this,"Pounds must be less than 500",Toast.LENGTH_LONG).show();
}
}
if(kiloToLb.isChecked()){
if(weightEntered <= 225) {
convertedWeight = weightEntered * conversionRate;
result.setText(tenth.format(convertedWeight) + "pounds");
} else {
Toast.makeText(MainActivity.this,"Kilos must be less than 225",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
答案 0 :(得分:1)
在您的清单中,您正在使用android:theme="@style/AppTheme.NoActionBar"
,因此主要活动将不包含操作栏,您将获得NullPointerException。
assert getSupportActionBar() != null
抛出异常,因为在这种情况下它始终为null。在这种情况下,Assert的工作方式如下:
if(!getSupportActionBar() != null) {
throw new Exception(); //NullPointerException in this case
}