2016-07-27 69 views
-4

我做了一个基本的android程序作为一个写作练习。但是只要打开它就会崩溃。我在手机上试了一下,但可悲的是我的android studio模拟器在我的电脑上无法正常工作,所以我的调试功能不是很多。它建立良好,所以我猜它不是一个语法错误或什么。

的AndroidManifest.xml:Android的计算器程序崩溃

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.net.crackerjacksystems.talkies"> 

    <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"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 



activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:theme="@style/AppTheme" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
<Button 
    android:id="@+id/Zero" 
    android:layout_width="260dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="@string/Button0" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/One" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/Zero" 
    android:text="@string/Button1" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Two" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/Zero" 
    android:layout_toRightOf="@+id/One" 
    android:layout_toEndOf="@+id/One" 
    android:text="@string/Button2" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Three" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/Zero" 
    android:layout_toRightOf="@+id/One" 
    android:layout_toEndOf="@+id/Two" 
    android:text="@string/Button3" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Four" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/One" 
    android:text="@string/Button4" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Five" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/One" 
    android:layout_toRightOf="@id/Four" 
    android:layout_toEndOf="@id/Four" 
    android:text="@string/Button5" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Six" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/One" 
    android:layout_toRightOf="@id/Five" 
    android:layout_toEndOf="@id/Five" 
    android:text="@string/Button6" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Seven" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Four" 
    android:text="@string/Button7" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Eight" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Five" 
    android:layout_toRightOf="@id/Seven" 
    android:layout_toEndOf="@id/Seven" 
    android:text="@string/Button8" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Nine" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Six" 
    android:layout_toRightOf="@id/Eight" 
    android:layout_toEndOf="@id/Eight" 
    android:text="@string/Button9" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Add" 
    android:layout_width="96dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Seven" 
    android:text="@string/ButtonAdd" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Subtract" 
    android:layout_width="96dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Eight" 
    android:layout_toRightOf="@id/Add" 
    android:layout_toEndOf="@id/Add" 
    android:text="@string/ButtonSubtract" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Multiply" 
    android:layout_width="96dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Eight" 
    android:layout_toRightOf="@id/Subtract" 
    android:layout_toEndOf="@id/Subtract" 
    android:text="@string/ButtonMultiply" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Divide" 
    android:layout_width="98dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Eight" 
    android:layout_toRightOf="@id/Multiply" 
    android:layout_toEndOf="@id/Multiply" 
    android:text="@string/ButtonDivide" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Clear" 
    android:layout_width="192dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Add" 
    android:text="@string/ButtonClear" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/AllClear" 
    android:layout_width="192dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Add" 
    android:layout_toRightOf="@id/Clear" 
    android:layout_toEndOf="@id/Clear" 
    android:text="@string/ButtonAllClear" 
    android:textSize="40sp"/> 
<Button 
    android:id="@+id/Equals" 
    android:layout_width="192dp" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/Zero" 
    android:layout_toEndOf="@id/Zero" 
    android:layout_alignParentBottom="true" 
    android:text="@string/ButtonEqual" 
    android:textSize="40sp"/> 
<TextView 
    android:id="@+id/Screen" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/Clear" 
    android:textSize="82sp" 
    /> 
</RelativeLayout> 



MainActivity.java:

package com.net.crackerjacksystems.talkies; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
int first = 0, second = 0; 
boolean onFirst = true; 
char op; 
Button zero = (Button)findViewById(R.id.Zero); 
Button one = (Button)findViewById(R.id.One); 
Button two = (Button)findViewById(R.id.Two); 
Button three = (Button)findViewById(R.id.Three); 
Button four = (Button)findViewById(R.id.Four); 
Button five = (Button)findViewById(R.id.Five); 
Button six = (Button)findViewById(R.id.Six); 
Button seven = (Button)findViewById(R.id.Seven); 
Button eight = (Button)findViewById(R.id.Eight); 
Button nine = (Button)findViewById(R.id.Nine); 
Button add = (Button)findViewById(R.id.Add); 
Button sub = (Button)findViewById(R.id.Subtract); 
Button mul = (Button)findViewById(R.id.Multiply); 
Button div = (Button)findViewById(R.id.Divide); 
Button cl = (Button)findViewById(R.id.Clear); 
Button acl = (Button)findViewById(R.id.AllClear); 
Button eql = (Button)findViewById(R.id.Equals); 
TextView screen = (TextView)findViewById(R.id.Screen); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    main(); 
} 
void main(){ 
    screen.setText(first); 
    zero.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10);screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    one.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+1;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    two.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+2;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    three.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+3;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    four.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+4;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    five.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+5;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    six.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+6;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    seven.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+7;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    eight.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+8;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    nine.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(onFirst){first = (first*10)+9;screen.setText(first);} 
      else{second = (second*10);screen.setText(second);} 
     } 
    }); 
    add.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      op = '+'; 
      onFirst = false; 
     } 
    }); 
    sub.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      op = '-'; 
      onFirst = false; 
     } 
    }); 
    mul.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      op = '*'; 
      onFirst = false; 
     } 
    }); 
    div.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      op = '/'; 
      onFirst = false; 
     } 
    }); 
    cl.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      if(onFirst){first = 0;} 
      else{second = 0;} 
     } 
    }); 
    acl.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      screen.setText(null); 
      first = 0; 
      second = 0; 
     } 
    }); 
    eql.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(op == '+'){screen.setText(first+second);} 
      if(op == '-'){screen.setText(first-second);} 
      if(op == '*'){screen.setText(String.valueOf(first*second));} 
      if(op == '/'){screen.setText(String.valueOf(first/second));} 
     } 
    }); 
} 
} 



的strings.xml:

<resources> 
<string name="app_name">Calculator</string> 
<string name="Button1">1</string> 
<string name="Button2">2</string> 
<string name="Button3">3</string> 
<string name="Button4">4</string> 
<string name="Button5">5</string> 
<string name="Button6">6</string> 
<string name="Button7">7</string> 
<string name="Button8">8</string> 
<string name="Button9">9</string> 
<string name="Button0">0</string> 
<string name="ButtonAdd">+</string> 
<string name="ButtonSubtract">—</string> 
<string name="ButtonMultiply">✕</string> 
<string name="ButtonDivide">÷</string> 
<string name="ButtonClear">C</string> 
<string name="ButtonAllClear">AC</string> 
<string name="ButtonEqual">=</string> 
</resources> 

只是为了澄清,它是一个非常简单的计算器。
如果有人可以帮忙解释为什么会崩溃,那会很好。

+0

从logcat发布您的堆栈跟踪。 –

+0

日志中的异常的堆栈跟踪 –

+0

可能的重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-我怎么办 - 它) –

回答

1

将您所有的findViewById()来电移动到您的onCreate()之内,因为在您致电setContentView()之前无法“查找”。

private Button zero, one, two, three, four, five, six, seven, eight, nine, add, sub, mul, div, cl, acl, eql; 
private TextView screen; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    zero = (Button)findViewById(R.id.Zero); 
    one = (Button)findViewById(R.id.One); 
    two = (Button)findViewById(R.id.Two); 
    three = (Button)findViewById(R.id.Three); 
    four = (Button)findViewById(R.id.Four); 
    five = (Button)findViewById(R.id.Five); 
    six = (Button)findViewById(R.id.Six); 
    seven = (Button)findViewById(R.id.Seven); 
    eight = (Button)findViewById(R.id.Eight); 
    nine = (Button)findViewById(R.id.Nine); 
    add = (Button)findViewById(R.id.Add); 
    sub = (Button)findViewById(R.id.Subtract); 
    mul = (Button)findViewById(R.id.Multiply); 
    div = (Button)findViewById(R.id.Divide); 
    cl = (Button)findViewById(R.id.Clear); 
    acl = (Button)findViewById(R.id.AllClear); 
    eql = (Button)findViewById(R.id.Equals); 
    screen = (TextView)findViewById(R.id.Screen); 

    main(); 
} 
+0

但是,当我去主要不会超出范围?我认为最好在开始时声明所有对象,然后在onCreate中初始化它。 –

+0

更新了我的答案 – Bill