自定义TextView错误

时间:2014-06-05 13:35:26

标签: android

我创建了一个类myTextView当我尝试为同一个类创建一个对象时,它扩展了TextView: 动态地工作:myTextView mytv = new myTextView(this.MainActivity);有用。 但 myTextView mtv =(myTextView)findViewById(R.id.mytV);

它出错了。请参阅Log Cat输出。 即使我将xml中的标记从TextView更改为它也无效。 我是android的新手。 PLS。救命。 代码:

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):致命异常:主要 06-05 18:41:13.611:E / AndroidRuntime(24106):进程:com.example.exttest,PID:24106 06-05 18:41:13.611:E / AndroidRuntime(24106):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exttest / com.example.exttest.MainActivity}:java.lang.ClassCastException:android .widget.TextView无法强制转换为com.example.exttest.myTextView 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.os.Looper.loop(Looper.java:136) 06-05 18:41:13.611:E / AndroidRuntime(24106):在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):引起:java.lang.ClassCastException:android.widget.TextView无法强制转换为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):在android.app.Activity.performCreate(Activity.java:5231) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:41:13.611:E / AndroidRuntime(24106):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:41:13.611:E / AndroidRuntime(24106):... 11更多 06-05 18:42:53.619:E / AndroidRuntime(24201):致命异常:主要 06-05 18:42:53.619:E / AndroidRuntime(24201):进程:com.example.exttest,PID:24201 06-05 18:42:53.619:E / AndroidRuntime(24201):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exttest / com.example.exttest.MainActivity}:android.view.InflateException:Binary XML文件行#11:使类com.example.exttest.myTextView出错 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.os.Looper.loop(Looper.java:136) 06-05 18:42:53.619:E / AndroidRuntime(24201):在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):引起:android.view.InflateException:二进制XML文件行#11:错误膨胀类com.example.exttest.myTextView 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.view.LayoutInflater.createView(LayoutInflater.java:603) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-05 18:42:53.619:E / AndroidRuntime(24201):在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):在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):在android.app.Activity.performCreate(Activity.java:5231) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:42:53.619:E / AndroidRuntime(24201):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:42:53.619:E / AndroidRuntime(24201):... 11更多 06-05 18:42:53.619:E / AndroidRuntime(24201):引起: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):在android.view.LayoutInflater.createView(LayoutInflater.java:568) 06-05 18:42:53.619:E / AndroidRuntime(24201):... 22更多 06-05 18:54:41.590:E / AndroidRuntime(24539):致命异常:主要 06-05 18:54:41.590:E / AndroidRuntime(24539):进程:com.example.exttest,PID:24539 06-05 18:54:41.590:E / AndroidRuntime(24539):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exttest / com.example.exttest.MainActivity}:android.view.InflateException:Binary XML文件行#12:错误类com.example.exttest.myTextView 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.os.Handler.dispatchMessage(Handler.java:102) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.os.Looper.loop(Looper.java:136) 06-05 18:54:41.590:E / AndroidRuntime(24539):在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):引起:android.view.InflateException:二进制XML文件行#12:错误输出类com.example.exttest.myTextView 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.view.LayoutInflater.createView(LayoutInflater.java:603) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.view.LayoutInflater.inflate(LayoutInflater.java:492) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.view.LayoutInflater.inflate(LayoutInflater.java:397) 06-05 18:54:41.590:E / AndroidRuntime(24539):在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):在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):在android.app.Activity.performCreate(Activity.java:5231) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-05 18:54:41.590:E / AndroidRuntime(24539):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-05 18:54:41.590:E / AndroidRuntime(24539):... 11更多 06-05 18:54:41.590:E / AndroidRuntime(24539):引起: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):在android.view.LayoutInflater.createView(LayoutInflater.java:568) 06-05 18:54:41.590:E / AndroidRuntime(24539):... 22更多

1 个答案:

答案 0 :(得分:0)

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