我有一个Android运行时致命异常:主要,我不知道我要做些什么来解决这个问题。我有以下代码,导致此异常:
package com.example.toggle_button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton toggleButton1,toggleButton2;
Button Submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
Submit =(Button)findViewById(R.id.textView1);
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
result.append("ToggleButton1 : ").append(toggleButton1.getText());
result.append("\nToggleButton2 : ").append(toggleButton2.getText());
//Displaying the message in toast
Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
}
});
}
@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;
}
}
在运行我的android项目之后的这段代码中,我在AVD上遇到错误,toggle_button不幸停止了,并且在运行时也显示了主要的致命异常。我无法解决这个问题。请告诉我适合的解决方案。< / p>
This is the LogCat of Exception:
07-13 03:38:13.793: D/AndroidRuntime(1362): Shutting down VM
07-13 03:38:13.793: W/dalvikvm(1362): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-13 03:38:13.815: E/AndroidRuntime(1362): FATAL EXCEPTION: main
07-13 03:38:13.815: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toggle_button/com.example.toggle_button.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.os.Looper.loop(Looper.java:137)
07-13 03:38:13.815: E/AndroidRuntime(1362): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-13 03:38:13.815: E/AndroidRuntime(1362): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 03:38:13.815: E/AndroidRuntime(1362): at java.lang.reflect.Method.invoke(Method.java:525)
07-13 03:38:13.815: E/AndroidRuntime(1362): at com.android.internal.
os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
答案 0 :(得分:4)
Logcat明确表示
:threadid = 1:线程退出,未捕获异常 (组= 0x41465700)07-13 03:38:13.815:E / AndroidRuntime(1362):
致命异议:主07-13 03:38:13.815:E / AndroidRuntime(1362):
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.toggle_button / com.example.toggle_button.MainActivity}: java.lang.ClassCastException:无法转换android.widget.TextView 到android.widget.Button
这里错了
Submit =(Button)findViewById(R.id.textView1);
喜欢
Submit =(Button)findViewById(R.id.buttonIdHere);
答案 1 :(得分:0)
您正在将TextView转换为Button:
ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
要解决此问题,您需要更改演员:
TextView Submit;
Submit =(TextView)findViewById(R.id.textView1);
或将layout.xml
文件中的UI类型更改为Button
答案 2 :(得分:0)
问题是由于非法typecasting
:
java.lang.ClassCastException:
android.widget.TextView
不能强制转换到android.widget.Button
如果View
为Button
:
Button Submit;
Submit = (Button)findViewById(R.id.button1);
如果您的View
确实是TextView
TextView textSubmit;
textSubmit = (TextView)findViewById(R.id.textView1);
这是继承:
java.lang.Object
主类
android.view.View
android.widget.TextView
android.widget.Button
因此,Button
扩展了TextView
。