我剛剛開始使用Android和Java,並且我剛剛製作了一個帶有幾個文字瀏覽,按鈕和edittext的簡單應用程序。ADT簡單應用程序 - 致命異常
當我運行應用程序時,出現錯誤消息:不幸的是,FirstApp已停止。 我熟悉編程,並想知道如果我錯過了一些庫,或者ADT配置錯誤。
我試圖清理並重新生成,要在Package explorer和Android Tools - >添加支持庫中右鍵單擊項目,重新啓動模擬器,重新啓動ADT並重新啓動計算機。不能弄清楚這是什麼。
您可以在這裏看到什麼錯嗎?
MainActivity.java進口
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.os.Build;
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCalc = (Button)findViewById(R.id.btnCalc);
btnCalc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText number = (EditText)findViewById(R.id.num);
TextView display = (TextView)findViewById(R.id.display);
double num = Double.parseDouble(number.getText().toString());
num = num * 5;
display.setText(num + "");
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.danlevi.testproject.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/lblTitle" />
<EditText
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="numberDecimal|numberSigned" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/num"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/btnCalc" />
<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnCalc"
android:layout_marginTop="16dp" />
</RelativeLayout>
控制檯
[2014-05-16 18:33:16 - TestProject] Dx
trouble writing output: already prepared
[2014-05-16 18:33:17 - TestProject] ------------------------------
[2014-05-16 18:33:17 - TestProject] Android Launch!
[2014-05-16 18:33:17 - TestProject] adb is running normally.
[2014-05-16 18:33:17 - TestProject] Performing com.danlevi.testproject.MainActivity activity launch
[2014-05-16 18:33:17 - TestProject] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Android'
[2014-05-16 18:33:17 - TestProject] Uploading TestProject.apk onto device 'emulator-5554'
[2014-05-16 18:33:18 - TestProject] Installing TestProject.apk...
[2014-05-16 18:33:30 - TestProject] Success!
[2014-05-16 18:33:30 - TestProject] Starting activity com.danlevi.testproject.MainActivity on device emulator-5554
[2014-05-16 18:33:33 - TestProject] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.danlevi.testproject/.MainActivity }
logcat的
05-16 12:33:37.412: D/AndroidRuntime(1579): Shutting down VM
05-16 12:33:37.412: W/dalvikvm(1579): threadid=1: thread exiting with uncaught exception (group=0xb2b08ba8)
05-16 12:33:37.432: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-16 12:33:37.432: E/AndroidRuntime(1579): Process: com.danlevi.testproject, PID: 1579
05-16 12:33:37.432: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danlevi.testproject/com.danlevi.testproject.MainActivity}: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.os.Handler.dispatchMessage(Handler.java:102)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.os.Looper.loop(Looper.java:136)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 12:33:37.432: E/AndroidRuntime(1579): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579): at java.lang.reflect.Method.invoke(Method.java:515)
05-16 12:33:37.432: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 12:33:37.432: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 12:33:37.432: E/AndroidRuntime(1579): at dalvik.system.NativeStart.main(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579): at com.danlevi.testproject.MainActivity.onCreate(MainActivity.java:25)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.Activity.performCreate(Activity.java:5231)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-16 12:33:37.432: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-16 12:33:37.432: E/AndroidRuntime(1579): ... 11 more
05-16 12:33:44.352: I/Process(1579): Sending signal. PID: 1579 SIG: 9
複製所有內容fragment_main到activity_main.xml..This的方法解決這個問題.. – Lal
這是最簡單和最簡單的解決方案..但正確的做法是在我的答案中指定。首先嚐試我的評論,如果這有助於嘗試我的答案... – Lal
或替換'setContentView(R.layout.activity_main);'as setContentView R.layout.fragment_main);' – Lal