2014-02-24 42 views
0

登錄貓:我不斷收到致命異常:主要

02-23 18:47:36.399: E/AndroidRuntime(25571): FATAL EXCEPTION: main 
02-23 18:47:36.399: E/AndroidRuntime(25571): Process: com.example.calculator, PID: 25571 
02-23 18:47:36.399: E/AndroidRuntime(25571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.os.Handler.dispatchMessage(Handler.java:102) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.os.Looper.loop(Looper.java:136) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at java.lang.reflect.Method.invoke(Method.java:515) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at dalvik.system.NativeStart.main(Native Method) 
02-23 18:47:36.399: E/AndroidRuntime(25571): Caused by: java.lang.NullPointerException 
02-23 18:47:36.399: E/AndroidRuntime(25571): at com.example.calculator.MainActivity.onCreate(MainActivity.java:39) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.Activity.performCreate(Activity.java:5231) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-23 18:47:36.399: E/AndroidRuntime(25571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-23 18:47:36.399: E/AndroidRuntime(25571): ... 11 more 
02-23 18:47:48.010: D/AndroidRuntime(25647): Shutting down VM 

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/screen" 
    android:textSize="60sp" 
    android:id="@+id/screen" /> 

<Button 
    android:id="@+id/num0" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:text="@string/num0" /> 

<Button 
    android:id="@+id/num1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/screen" 
    android:layout_marginTop="16dp" 
    android:text="@string/num1" /> 

<Button 
    android:id="@+id/num2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num1" 
    android:layout_alignBottom="@+id/num1" 
    android:layout_marginLeft="16dp" 
    android:layout_toRightOf="@+id/num1" 
    android:text="@string/num2" /> 

<Button 
    android:id="@+id/num3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num2" 
    android:layout_alignBottom="@+id/num2" 
    android:layout_marginLeft="20dp" 
    android:layout_toRightOf="@+id/num2" 
    android:text="@string/num3" /> 

<Button 
    android:id="@+id/num4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toLeftOf="@+id/num2" 
    android:text="@string/num4" /> 

<Button 
    android:id="@+id/num5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num4" 
    android:layout_alignBottom="@+id/num4" 
    android:layout_alignLeft="@+id/num2" 
    android:text="@string/num5" /> 

<Button 
    android:id="@+id/num6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num5" 
    android:layout_alignBottom="@+id/num5" 
    android:layout_alignLeft="@+id/num3" 
    android:text="@string/num6" /> 

<Button 
    android:id="@+id/num7" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/num4" 
    android:layout_marginTop="65dp" 
    android:layout_toLeftOf="@+id/num2" 
    android:text="@string/num7" /> 

<Button 
    android:id="@+id/num8" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num7" 
    android:layout_alignBottom="@+id/num7" 
    android:layout_alignLeft="@+id/num5" 
    android:text="@string/num8" /> 

<Button 
    android:id="@+id/num9" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num8" 
    android:layout_alignBottom="@+id/num8" 
    android:layout_alignLeft="@+id/num6" 
    android:text="@string/num9" /> 

<Button 
    android:id="@+id/add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/num3" 
    android:layout_alignBottom="@+id/num3" 
    android:layout_alignParentRight="true" 
    android:text="@string/add" /> 

<Button 
    android:id="@+id/subtract" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/num9" 
    android:text="@string/subtract" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/multiply" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/num7" 
    android:layout_alignParentRight="true" 
    android:text="@string/multiply" /> 

<Button 
    android:id="@+id/divide" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:text="@string/divide" /> 

<Button 
    android:id="@+id/clear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toLeftOf="@+id/num3" 
    android:text="@string/clear" /> 

<Button 
    android:id="@+id/equal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/num9" 
    android:layout_alignParentBottom="true" 
    android:text="@string/equal" /> 

</RelativeLayout> 

MainActivity.java:

package com.example.calculator; 

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

public class MainActivity extends Activity { 

Button zero, one, two, three, four, five, six, seven, eight, nine, clear, add, subtract, multiply, divide, equal; 
double num1, num2, answer; 
String operation; 
int twoDigit; 
TextView screen; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    zero = (Button) findViewById(R.id.num0); 
    one = (Button) findViewById(R.id.num1); 
    two = (Button) findViewById(R.id.num2); 
    three = (Button) findViewById(R.id.num3); 
    four = (Button) findViewById(R.id.num4); 
    five = (Button) findViewById(R.id.num5); 
    six = (Button) findViewById(R.id.num6); 
    seven = (Button) findViewById(R.id.num7); 
    eight = (Button) findViewById(R.id.num8); 
    nine = (Button) findViewById(R.id.num9); 
    clear = (Button) findViewById(R.id.clear); 
    add = (Button) findViewById(R.id.add); 
    subtract = (Button) findViewById(R.id.subtract); 
    multiply = (Button) findViewById(R.id.multiply); 
    divide = (Button) findViewById(R.id.divide); 
    equal = (Button) findViewById(R.id.equal); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    screen.setText("0"); 
    num1 = 0; num2 = 0; 
    firstNumberEntry(); 
} 

public void firstNumberEntry() { 
    one.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      num1 = 1; 
      screen.setText("" + num1); 
      if(add.isPressed()){ 
       operation = "add"; 
       operations(num1, operation); 
      } else if(subtract.isPressed()){ 
       operation = "subtract"; 
       operations(num1, operation); 
      }else if(multiply.isPressed()){ 
       operation = "multiply"; 
       operations(num1, operation); 
      }else if(divide.isPressed()){ 
       operation = "divide"; 
       operations(num1, operation); 
      } 
     } 


    }); 


} 



public void operations(double num1, String operation) { 


} 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.calculator" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="19" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".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> 

我在Android開發初學者,我試圖做一個雙 - 位計算器。我還沒有完成,但我想測試我已經編碼。無論何時我在設備上打開應用程序,我都會收到消息「不幸的是,計算器已停止」。我附上了我的所有代碼,包括LogCat。既然我是這個東西的新手,我真的不明白什麼是錯的。任何幫助?

+0

將所有'findViewById'語句移動到'setContentView'下面。 –

+2

繼續閱讀日誌。它有一個「由com.example.calculator.MainActivity.onCreate(MainActivity.java:39)的」java.lang.NullPointerException異常「 – jprofitt

+0

啊,是的,對於這個特定的問題,這是因爲未初始化的'screen'變量。我仍然建議你在'setContentView'下移動每一個初始化來解決未來的問題。 +1 to @jprofitt和@Abrackadabra –

回答

4

您不要將screen變量設置爲任何東西,所以它被初始化爲null,當你嘗試調用一個null的方法,你會得到一個異常。

您應此語句前加上

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

+0

謝謝!馬上修復它! – Ved115