0
votes

I created a Class myTextView That's extends the TextView when i try to create an object for the same class : Dynamically it works : myTextView mytv = new myTextView(this.MainActivity); It works. but myTextView mtv = (myTextView)findViewById(R.id.mytV);

It gives an error. Refer the Log Cat Output. Even if i change tag in xml from TextView to it ain't works. I'm new to android. Pls. Help. code:

package com.example.exttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
    myTextView mtv  =   null;
    RelativeLayout rtv  =   null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rtv =   (RelativeLayout)findViewById(R.id.parent);
        mtv =   new myTextView(getApplicationContext());
        mtv.setText("Hey Bub");
        mtv =   (myTextView)findViewById(R.id.text1);
        //rtv.addView(mtv);
    }
    @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:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
   android:id="@+id/parent" >

  <com.example.exttest.myTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Text View"
        android:id="@+id/text1" />
</RelativeLayout>

LogCat:

06-05 18:41:13.611: E/AndroidRuntime(24106): FATAL EXCEPTION: main 06-05 18:41:13.611: E/AndroidRuntime(24106): Process: com.example.exttest, PID: 24106 06-05 18:41:13.611: E/AndroidRuntime(24106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exttest/com.example.exttest.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.example.exttest.myTextView 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.os.Looper.loop(Looper.java:136) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 18:41:13.611: E/AndroidRuntime(24106): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 18:41:13.611: E/AndroidRuntime(24106): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 18:41:13.611: E/AndroidRuntime(24106): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 18:41:13.611: E/AndroidRuntime(24106): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 18:41:13.611: E/AndroidRuntime(24106): at dalvik.system.NativeStart.main(Native Method) 06-05 18:41:13.611: E/AndroidRuntime(24106): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.example.exttest.myTextView 06-05 18:41:13.611: E/AndroidRuntime(24106): at com.example.exttest.MainActivity.onCreate(MainActivity.java:13) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.Activity.performCreate(Activity.java:5231) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:41:13.611: E/AndroidRuntime(24106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:41:13.611: E/AndroidRuntime(24106): ... 11 more 06-05 18:42:53.619: E/AndroidRuntime(24201): FATAL EXCEPTION: main 06-05 18:42:53.619: E/AndroidRuntime(24201): Process: com.example.exttest, PID: 24201 06-05 18:42:53.619: E/AndroidRuntime(24201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exttest/com.example.exttest.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.exttest.myTextView 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.os.Looper.loop(Looper.java:136) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 18:42:53.619: E/AndroidRuntime(24201): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 18:42:53.619: E/AndroidRuntime(24201): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 18:42:53.619: E/AndroidRuntime(24201): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 18:42:53.619: E/AndroidRuntime(24201): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 18:42:53.619: E/AndroidRuntime(24201): at dalvik.system.NativeStart.main(Native Method) 06-05 18:42:53.619: E/AndroidRuntime(24201): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.exttest.myTextView 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.createView(LayoutInflater.java:603) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 06-05 18:42:53.619: E/AndroidRuntime(24201): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.Activity.setContentView(Activity.java:1929) 06-05 18:42:53.619: E/AndroidRuntime(24201): at com.example.exttest.MainActivity.onCreate(MainActivity.java:12) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.Activity.performCreate(Activity.java:5231) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:42:53.619: E/AndroidRuntime(24201): ... 11 more 06-05 18:42:53.619: E/AndroidRuntime(24201): Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet] 06-05 18:42:53.619: E/AndroidRuntime(24201): at java.lang.Class.getConstructorOrMethod(Class.java:472) 06-05 18:42:53.619: E/AndroidRuntime(24201): at java.lang.Class.getConstructor(Class.java:446) 06-05 18:42:53.619: E/AndroidRuntime(24201): at android.view.LayoutInflater.createView(LayoutInflater.java:568) 06-05 18:42:53.619: E/AndroidRuntime(24201): ... 22 more 06-05 18:54:41.590: E/AndroidRuntime(24539): FATAL EXCEPTION: main 06-05 18:54:41.590: E/AndroidRuntime(24539): Process: com.example.exttest, PID: 24539 06-05 18:54:41.590: E/AndroidRuntime(24539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exttest/com.example.exttest.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.exttest.myTextView 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.os.Looper.loop(Looper.java:136) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-05 18:54:41.590: E/AndroidRuntime(24539): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 18:54:41.590: E/AndroidRuntime(24539): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 18:54:41.590: E/AndroidRuntime(24539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-05 18:54:41.590: E/AndroidRuntime(24539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-05 18:54:41.590: E/AndroidRuntime(24539): at dalvik.system.NativeStart.main(Native Method) 06-05 18:54:41.590: E/AndroidRuntime(24539): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.exttest.myTextView 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.createView(LayoutInflater.java:603) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 06-05 18:54:41.590: E/AndroidRuntime(24539): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.Activity.setContentView(Activity.java:1929) 06-05 18:54:41.590: E/AndroidRuntime(24539): at com.example.exttest.MainActivity.onCreate(MainActivity.java:14) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.Activity.performCreate(Activity.java:5231) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:54:41.590: E/AndroidRuntime(24539): ... 11 more 06-05 18:54:41.590: E/AndroidRuntime(24539): Caused by: java.lang.NoSuchMethodException: [class android.content.Context, interface android.util.AttributeSet] 06-05 18:54:41.590: E/AndroidRuntime(24539): at java.lang.Class.getConstructorOrMethod(Class.java:472) 06-05 18:54:41.590: E/AndroidRuntime(24539): at java.lang.Class.getConstructor(Class.java:446) 06-05 18:54:41.590: E/AndroidRuntime(24539): at android.view.LayoutInflater.createView(LayoutInflater.java:568) 06-05 18:54:41.590: E/AndroidRuntime(24539): ... 22 more

1

1 Answers

0
votes
your code should look like this 
        mtv =   (myTextView)findViewById(R.id.text1);
        mtv.setText("Hey Bub");
remove this line  mtv =   new myTextView(getApplicationContext());