我是Android的新手,今天正在创建我的第一个Android应用程序,但它无法启动。当我启动应用程序时它来了upp,但是一个弹出窗口说“不幸的是[appname]已经停止了”,它就会关闭。
我在youtube上关注了Derek Banas的教程,作为我的第一个应用程序,我创建了一个小费计算器。
当搞砸问题时,问题似乎每次都取决于不同的事情。
我不知道为您提供什么类型的代码,但我会发布我认为最重要的代码。
我有点困惑的一点是,我通过fragment_main创建了我的所有GUI,而不是我在每个指南中看到的activity_main! 另外,我正在使用android studio!
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.emilime93.tipcalc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.emilime93.tipcalc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是代码:
package com.emilime93.tipcalc;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private static final String FINAL_BILL = "FINAL_BILL";
private static final String CURRENT_TIP = "CURRENT_TIP";
private static final String PRE_TIP_BILL = "PRE_TIP_BILL";
private static final String NICE_WAITRESS = "NICE_WAITRESS";
private double finalBill;
private double preTipBill;
private double currentTip;
private Boolean niceWaitress;
private EditText finalBillET;
private EditText preTipBillET;
private CheckBox niceWaitressCB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
preTipBill = 0.0;
currentTip = 0.1;
finalBill = 0.0;
niceWaitress = false;
} else {
preTipBill = savedInstanceState.getDouble(PRE_TIP_BILL);
currentTip = savedInstanceState.getDouble(CURRENT_TIP);
finalBill = savedInstanceState.getDouble(FINAL_BILL);
niceWaitress = savedInstanceState.getBoolean(NICE_WAITRESS);
}
//adding actionlistener for the checkbutton
preTipBillET = (EditText) findViewById(R.id.billTextEdit);
finalBillET = (EditText) findViewById(R.id.finalBillTextEdit);
niceWaitressCB = (CheckBox) findViewById(R.id.niceWatiressCheckBox);
niceWaitressCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(niceWaitress) {
currentTip -= 0.05;
} else {
currentTip += 0.05;
}
niceWaitress = b;
updateBillAndTip();
}
});
//adding actionlistener to the input filed for the bill
preTipBillET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
try {
preTipBill = Double.parseDouble(charSequence.toString());
updateBillAndTip();
} catch(NumberFormatException e){
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
/*private TextWatcher billPreTipListener = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
try {
preTipBill = Double.parseDouble(s.toString());
} catch(NumberFormatException e){
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};*/
private void updateBillAndTip() {
this.finalBill = (currentTip+1)*preTipBill;
finalBillET.setText(String.format("%.02f", finalBill));
}
public void increaseTip(double precentageAmmount) {
this.currentTip += precentageAmmount;
}
protected void onSavedInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble(FINAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, currentTip);
outState.putDouble(PRE_TIP_BILL, preTipBill);
outState.putBoolean(NICE_WAITRESS, niceWaitress);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
启动应用时出现错误消息:
12-05 14:36:51.582 1092-1092/com.emilime93.tipcalc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.emilime93.tipcalc, PID: 1092
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emilime93.tipcalc/com.emilime93.tipcalc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.emilime93.tipcalc.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
布局activity_main
没有ID为niceWatiressCheckBox
的视图,findViewById()
返回null。尝试在空引用上调用setOnCheckedChangeListener()
会导致NPE。
答案 1 :(得分:0)
好吧,问题是当我的布局位于“fragment_main”时我使用了setContentView(R.layout.activity_main);
。所以我将其更改为setContentView(R.layout.fragment_main);
。
我不知道所有这些事情做了什么,这就是为什么我无法追踪它。我希望至少能帮助别人解决这个问题!
Stil不知道片段和活动_main之间的区别是什么!