2016-07-27 8 views
-4

私は筆記運動として基本的なアンドロイドプログラムを作った。しかし、それが開くたびにクラッシュします。私はそれを電話で試しましたが、悲しいことに私のアンドロイドスタジオエミュレータは私のコンピュータでは動作しませんので、私のデバッグ能力はあまりよくありません。それは文法エラーか何かではないと私は推測するので、上手く構築されます。

の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

onCreate()内のすべてのfindViewById()コールは、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

関連する問題