나는 같은 클래스에 대한 개체를 만들려고 TextView를 확장하는 myTextView 클래스를 만들었습니다. 동적으로 작동합니다. myTextView mytv = new myTextView (this.MainActivity); 그것은 작동합니다. 하지만 myTextView mtv = (myTextView) findViewById (R.id.mytV);사용자 지정 TextView 오류
오류가 발생합니다. Log Cat Output을 참조하십시오. xml의 태그를 TextView에서 태그로 변경하더라도 작동하지 않습니다. 나는 안드로이드를 처음 사용합니다. 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>
로그 캣 :
06-05 18 : 41 : 13.611 : E가/AndroidRuntime (24106) : FATAL EXCEPTION : 메인 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.Cl assCastException : android.widget.TextView를 com.example.exttest.myTextView로 캐스팅 할 수 없습니다. 06 : 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) : at 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/18 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/A ndroidRuntime (24106) : android.app.ActivityThread.main (ActivityThread.java:5017) 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106) : java.lang.reflect.Method.invokeNative (네이티브 메소드) 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106) : java.lang.reflect.Method.invoke (Method.java:515) 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106)) : com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106) : com.android.internal.os.ZygoteInit에서 전자 메일 주소 : 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106) : dalvik.system.NativeStart.main (네이티브 방식) 06-05 18 : 41 : 13.611 : 전자 메일 (ZygoteInit.java:595) AndroidRuntime (24106) : 원인 : java.lang.ClassCastException : android.widget.TextView를 com.example.exttest.myTextView로 캐스팅 할 수 없습니다. 06-05 18 : 41 : 13.611 : E/AndroidRuntime (24106) : 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 : 활동을 시작할 수 없습니다. ComponentIn fo com.example.exttest/com.example.exttest.MainActivity} android.view.InflateException : 이진 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/A ndroidRuntime (24201) : java.lang.reflect.Method.invokeNative (네이티브 메소드) 06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : java.lang.reflect.Method.invoke (Method.java) : 515) 06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) 06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595) 06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : at dalvik.system.NativeStart .inain (네이티브 메서드) 06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : 원인 : android.view.InflateException : 이진 XML 파일 줄 # 11 : inflatable error 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) : at 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) : 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) : 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 : [클래스 android.content.Context, 인터페이스 android.util.AttributeSet] 06-05 18:42 : 53.619 : E/AndroidRuntime (24201) : java.lang.Class.getConstructorOrMethod (Class.java:472)에서06-05 18 : 42 : 53.619 : E/AndroidRuntime (24201) : 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 자세히 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 : 이진 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) : java.lang.reflect.Method.invokeNati에서 ve (네이티브 메소드) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : java.lang.reflect.Method.invoke (Method.java:515) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : com.android.internal .os.ZygoteInit.main (ZygoteInit.java:595) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : at dalvik.system.NativeStart.main (기본 메소드) 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)) : at android.view.LayoutInflater.createView (LayoutInflater.java:603) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : andr oid.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) : 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 (2 4539) : 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) : at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2159) 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : ... 더 자세히 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : 원인 : java.lang.NoSuchMethodException : [클래스 android.content.Context, 인터페이스 android.util.AttributeSet] 06-05 18 : 54 : 41.590 : E/AndroidRuntime (24539) : java.lang .Class.getConstructorOrMethod (Class.java:472) 06-05 18 : 54 : 41.590 : E/AndroidRunti (24539) : 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) : ... 더
내가 라인이 작동 동적으로 보여주기 위해 쓴 BUB (22). findViewbyId가 new myTextView (getApplicationContext()) 행을 제거하지 않습니다. –
myTextView의 개체를 만들 필요가 없습니다. 위의 두 줄과 같이 XML에서 가져올 수 있습니다. 동적으로 표시하려면 mtv를 참조하십시오. 그래서 새로운 myTextView (getApplicationContext()); –