my java class consists of following code
package com.example.sati;
import com.example.sati.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int count;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count=0;
add=(Button)findViewById(R.id.but);
sub=(Button)findViewById(R.id.butone);
display=(Button)findViewById(R.id.tvdis);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count++;
display.setText("your total is"+count);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count--;
display.setText("your total is"+count);
}
});
}
@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;
}
}
我的xml文件代码如下;
<LinearLayout 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"
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=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvdis"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/tot"
android:textSize="25sp"
android:layout_gravity="center"
android:id="@+id/but"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/tet"
android:textSize="25sp"
android:layout_gravity="center"
android:id="@+id/butone" />
</LinearLayout>
但是当我运行此应用程序时,它会显示消息“您的应用程序sati已意外停止。请在模拟器上再次尝试[强制关闭]。
Log cat显示以下错误;
03-23 01:10:30.042: D/AndroidRuntime(476): Shutting down VM
03-23 01:10:30.042: W/dalvikvm(476): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-23 01:10:30.072: E/AndroidRuntime(476): FATAL EXCEPTION: main
03-23 01:10:30.072: E/AndroidRuntime(476): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sati/com.example.sati.MainActivity}: java.lang.ClassCastException: android.widget.TextView
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.os.Looper.loop(Looper.java:123)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-23 01:10:30.072: E/AndroidRuntime(476): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 01:10:30.072: E/AndroidRuntime(476): at java.lang.reflect.Method.invoke(Method.java:521)
03-23 01:10:30.072: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-23 01:10:30.072: E/AndroidRuntime(476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-23 01:10:30.072: E/AndroidRuntime(476): at dalvik.system.NativeStart.main(NativeMethod)
//应用程序包含两个按钮add和sub在按下时添加1加上当按下sub时减去一个,并且数字的增量和减少显示为按钮上方的文本
03-23 01:10:30.072: E/AndroidRuntime(476): Caused by: java.lang.ClassCastException: android.widget.TextView
03-23 01:10:30.072: E/AndroidRuntime(476): at com.example.sati.MainActivity.onCreate(MainActivity.java:26)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 01:10:30.072: E/AndroidRuntime(476): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-23 01:10:30.072: E/AndroidRuntime(476): ... 11 more
03-23 01:15:30.154: I/Process(476): Sending signal. PID: 476 SIG: 9
答案 0 :(得分:2)
display=(Button)findViewById(R.id.tvdis);
应该是
display=(TextView)findViewById(R.id.tvdis);
您将其声明为TextView
但尝试将其转换为Button
。这是您的例外Caused by: java.lang.ClassCastException: android.widget.TextView
同时删除setContentView
和onCreate
的一次通话。无需再打两次电话。