1
我正在計算器,但啓動後,當我按下計算器的任何按鈕(0,1,2等)。我不幸計算器停止工作。在logcat中得到錯誤..我在哪裏缺乏?計算器:錯誤沒有這樣的文件或目錄,跳過幀
CODE:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Calculator extends Activity implements OnClickListener {
String initialnum="0";
TextView textview;
char operator=' ';
int result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
TextView textview= (TextView) findViewById(R.id.textView1);
textview.setText("0");
((Button) findViewById(R.id.button00)).setOnClickListener(this);
((Button) findViewById(R.id.button01)).setOnClickListener(this);
((Button) findViewById(R.id.button02)).setOnClickListener(this);
((Button) findViewById(R.id.button03)).setOnClickListener(this);
((Button) findViewById(R.id.button04)).setOnClickListener(this);
((Button) findViewById(R.id.button05)).setOnClickListener(this);
((Button) findViewById(R.id.button06)).setOnClickListener(this);
((Button) findViewById(R.id.button07)).setOnClickListener(this);
((Button) findViewById(R.id.button08)).setOnClickListener(this);
((Button) findViewById(R.id.button09)).setOnClickListener(this);
((Button) findViewById(R.id.buttonc)).setOnClickListener(this);
((Button) findViewById(R.id.buttondivide)).setOnClickListener(this);
((Button) findViewById(R.id.buttonequal)).setOnClickListener(this);
((Button) findViewById(R.id.buttonminus)).setOnClickListener(this);
((Button) findViewById(R.id.buttonmultiply)).setOnClickListener(this);
((Button) findViewById(R.id.buttonplus)).setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button00:
case R.id.button01:
case R.id.button02:
case R.id.button03:
case R.id.button04:
case R.id.button05:
case R.id.button06:
case R.id.button07:
case R.id.button08:
case R.id.button09:
String digit= ((Button) v).getText().toString();
if (initialnum.equals("0")) {
initialnum= digit;
} else {
initialnum +=digit;
}
textview.setText(initialnum);
if (operator == '=') {
result= 0;
operator=' ';
} break;
case R.id.buttonc:
result=0;
operator=' ';
case R.id.buttondivide:
compute();
operator='/';
break;
case R.id.buttonminus:
compute();
operator='-';
break;
case R.id.buttonmultiply:
compute();
operator='*';
break;
case R.id.buttonplus:
compute();
operator='+';
break;
case R.id.buttonequal:
compute();
operator='=';
break;
}
}
private void compute() {
int number= Integer.parseInt(initialnum);
// TODO Auto-generated method stub
if (operator==' ') {
result= number;
} else if (operator=='+') {
result+= number;
}else if (operator=='-') {
result-= number;
}else if (operator=='*') {
result*= number;
} else if (operator=='/') {
result/= number;
}
textview.setText(result);
}
}
錯誤日誌:
03-05 13:10:52.539: E/Trace(805): error opening trace file: No such file or directory (2)
03-05 13:10:53.359: I/Choreographer(805): Skipped 55 frames! The application may be doing too much work on its main thread.
03-05 13:10:53.399: D/gralloc_goldfish(805): Emulator without GPU emulation detected.
03-05 13:14:46.659: D/AndroidRuntime(805): Shutting down VM
03-05 13:14:46.669: W/dalvikvm(805): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-05 13:14:46.699: E/AndroidRuntime(805): FATAL EXCEPTION: main
03-05 13:14:46.699: E/AndroidRuntime(805): java.lang.NullPointerException
03-05 13:14:46.699: E/AndroidRuntime(805): at com.example.calculator.Calculator.onClick(Calculator.java:64)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.view.View.performClick(View.java:4084)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.view.View$PerformClick.run(View.java:16966)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.os.Handler.handleCallback(Handler.java:615)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.os.Looper.loop(Looper.java:137)
03-05 13:14:46.699: E/AndroidRuntime(805): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-05 13:14:46.699: E/AndroidRuntime(805): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:14:46.699: E/AndroidRuntime(805): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 13:14:46.699: E/AndroidRuntime(805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-05 13:14:46.699: E/AndroidRuntime(805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-05 13:14:46.699: E/AndroidRuntime(805): at dalvik.system.NativeStart.main(Native Method)
佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:gravity="right|top"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#cccccc"
android:gravity="right"
android:paddingTop="10dp"
android:text="TextView"
android:textSize="25dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:layout_marginTop="10dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/button09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<Button
android:id="@+id/buttondivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/buttonmultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/buttonminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/buttonc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="c" />
<Button
android:id="@+id/buttonequal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="=" />
<Button
android:id="@+id/buttonplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</TableRow>
</TableLayout>
把你的佈局文件也請... – rajshree
@rajshree:新增佈局file.but我不認爲錯誤是佈局文件。 – apurva