Code Android bị lỗi RuntimeException: Unable to start activity

Chả là chả hiểu sao chạy Android Studio lại bị lỗi này . Chả biết bị lm sao nữa . Nhưng viết Hello World vẫn chạy Bt mà nếu code thêm mắm thêm muối vào tý lại bị thế này . Bác nào pro chỉ E vs:

Code :

package demo.bk.trungson.demo1;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView tex;
    EditText edtSo1,edtSo2;
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtSo1=(EditText)findViewById(R.id.textView1);
        edtSo2=(EditText)findViewById(R.id.textView2);
        tex=(TextView)findViewById(R.id.textView3);
        btn=(Button)findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String chuoi1=edtSo1.getText().toString();
                int so1= Integer.parseInt(chuoi1);
                String chuoi2=edtSo2.getText().toString();
                int so2=Integer.parseInt(chuoi2);

                int tong=so1+so2;

                tex.setText(String.valueOf(tong));
            }
        });




    }
}

Error:

12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime: FATAL EXCEPTION: main
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{demo.bk.trungson.demo1/demo.bk.trungson.demo1.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:  Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at demo.bk.trungson.demo1.MainActivity.onCreate(MainActivity.java:23)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5133)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-09 10:28:49.100 1414-1414/demo.bk.trungson.demo1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

Có gì đó không ổn :smile:

bị sao thế hả b . B chỉ rõ hơn đc ko

Lỗi ở chỗ này . Em check lại xem cái này ép chuẩn chưa

edtSo1=(EditText)findViewById(R.id.textView1);
edtSo2=(EditText)findViewById(R.id.textView2);

http://upques.com/blogs/thanhthinh

E học cả code y nguyên thầy KhoaPham trên youtube mà . Xem code thì chả khác gì ạ . Chả hiểu sao thầy trên đấy chạy đc mà mình ko chạy đc

TextView sao bạn lại ép về EditText ?
Khi học lập trình có một điều nên tránh là luôn nghĩ mình đúng. :smile:

2 Likes

thanks Bro . E kéo nhầm cái plain textview thay vì plain text :v

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?