简体   繁体   English

我的代码给出了运行时错误! 我是android编程的新手,谁能告诉我我的错误

[英]My code is giving runtime error! i am new to android programming can anyone tell my mistake

Can anyone explain what are errors in my android programme? 谁能解释我的Android程序中有什么错误?

Logcat : Logcat:

07-13 09:04:18.429: D/AndroidRuntime(274): Shutting down VM

07-13 09:04:18.429: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

07-13 09:04:18.469: E/AndroidRuntime(274): FATAL EXCEPTION: main

07-13 09:04:18.469: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstapplication1/com.example.firstapplication1.StartingPoint}: java.lang.ClassCastException: android.widget.TextView

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)

07-13 09:04:18.469: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)

07-13 09:04:18.469: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

07-13 09:04:18.469: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)

07-13 09:04:18.469: E/AndroidRuntime(274): Caused by: java.lang.ClassCastException: android.widget.TextView

07-13 09:04:18.469: E/AndroidRuntime(274):  at com.example.firstapplication1.StartingPoint.onCreate(StartingPoint.java:25)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

07-13 09:04:18.469: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

07-13 09:04:18.469: E/AndroidRuntime(274):  ... 11 more
07-13 09:04:28.659: I/Process(274): Sending signal. PID: 274 SIG: 9

StartingPoint.java : StartingPoint.java:

public class StartingPoint extends Activity {
int counter;
Button add;
Button sub;
TextView display;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    counter=0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (Button) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            counter++;
            display.setText("Your Total is " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            counter--;
            display.setText("Your Total is " + counter);
        }
    });

}


public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.starting_point, menu);
    return true;
}

 }

You get a ClassCastException because you are casting your TextView to a Button here: 之所以会收到ClassCastException是因为您在这里将TextView强制转换为Button

display = (Button) findViewById(R.id.tvDisplay);

You need to cast it to a TextView : 您需要将其TextView转换为TextView

display = (TextView) findViewById(R.id.tvDisplay);

Change this 改变这个

      display = (Button) findViewById(R.id.tvDisplay);
     // casting to textview to button. wrong

To

       display = (TextView) findViewById(R.id.tvDisplay);

Caused by: java.lang.ClassCastException: android.widget.TextView

Your display is TextView while initializing you have cast the same with Button 您的显示为TextView ,初始化时已使用Button了相同的显示

Change the following line in your code 更改代码中的以下行

display = (Button) findViewById(R.id.tvDisplay);

to

display = (TextView) findViewById(R.id.tvDisplay);

You cannot cast TextView to Button 您不能将TextView强制转换为Button

Change 更改

display = (Button) findViewById(R.id.tvDisplay);

to

display = (TextView) findViewById(R.id.tvDisplay);

This is a very simple mistake and it happens. 这是一个非常简单的错误,并且会发生。 Please update your IDE and ADT to latest because the new ADT catches these mistakes at coding time. 请更新您的IDE和ADT至最新,因为新的ADT在编码时会捕获这些错误。 It will save lot of your time in learning. 这将节省您的大量学习时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从以下代码中获取注册ID值为null,以使用Android实施C2DM。 ,有人可以告诉我这里做错了什么吗? - Getting Registration id value as null from the following code to implement C2DM using Android. ,Can anyone pls tell me what mistake am doing here ? Android,请告诉我我的错误 - Android, tell me my mistake please 谁能告诉我有关Android的InternalStorage的信息? 我很困惑 - Can anyone tell me about InternalStorage in Android ? I am confused 我在Android应用中的Google地图上显示用户的当前位置时遇到问题。 谁能告诉我我该如何处理我的代码 - I have an issue displaying the user's current location on Google Maps in my Android app. Can anyone tell me what I need to do with my code 我无法在数组列表中绑定联系人,有人可以告诉我我做错了什么吗? - I cannot bind the contacts in my arraylist can anyone tell me what i do wrong?ANDROID 我从Firebase获取数据并在TextView中显示它但我收到了一些错误。 任何人都可以检查我的代码并建议我 - I am getting data from Firebase and showing it in the TextView but I am getting some error. Can anyone please check out my code and suggest me 任何人都可以告诉我我的 Android Studio Java 代码有什么问题...正在尝试实现底部导航栏 - Can Anyone Tell me whats the problem in my Android Studio Java Code... Trying to implement bottom Navigation Bar 为什么我的Android代码中出现空指针错误? - Why am I getting a null pointer error in my Android Code? 任何人都可以帮助我了解在 Android 中使用 AsyncTask 时出现错误代码 400 - Can anyone help me about I am getting error code 400 while working with AsyncTask in Android 我是android Java的初学者。 谁能解释代码 - I am beginner in android Java. Can anyone explain the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM