Android: ошибка расширенного класса - PullRequest
0 голосов
/ 13 марта 2012

Я пытаюсь использовать автоматическое изменение размера, указанное в Автоматическое масштабирование текста TextView, чтобы поместиться в пределах , но каким-то образом я пытаюсь реализовать его, как показано:

AutoResizeTextView test;
Boolean fit = true;
@Override
public void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
    test =  new AutoResizeTextView(this);
    if (winMan != null)
    {
        int orientationS = winMan.getDefaultDisplay().getOrientation();   
        if (orientationS == 0) 
        {
            test = (AutoResizeTextView)findViewById(R.id.testing123);
            test.setTextColor(R.color.red); 
        test.resizeText();
            // Portrait
            setContentView(R.layout.content_page);          
            //addListenerOnButton();
        }

Но это всегда приводит к следующей ошибке, когда я хочу редактировать любой текст / пытаться редактировать что-либо, связанное с текстом. Например, test.resizeText ();

03-13 08:13:38.281: W/dalvikvm(2002): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-13 08:13:38.292: E/AndroidRuntime(2002): FATAL EXCEPTION: main
03-13 08:13:38.292: E/AndroidRuntime(2002): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.ContentPage}: java.lang.NullPointerException
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.os.Looper.loop(Looper.java:123)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at     java.lang.reflect.Method.invokeNative(Native Method)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at java.lang.reflect.Method.invoke(Method.java:521)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at dalvik.system.NativeStart.main(Native Method)
03-13 08:13:38.292: E/AndroidRuntime(2002): Caused by: java.lang.NullPointerException
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.mypackage.ContentPage.onCreate(ContentPage.java:41)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 08:13:38.292: E/AndroidRuntime(2002):     ... 11 more
03-13 08:13:40.142: I/Process(2002): Sending signal. PID: 2002 SIG: 9

новый журнал после внедрения кодов имранхана

03-13 08:13:38.281: W/dalvikvm(2002): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-13 08:13:38.292: E/AndroidRuntime(2002): FATAL EXCEPTION: main
03-13 08:13:38.292: E/AndroidRuntime(2002): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.ContentPage}: java.lang.NullPointerException
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.os.Looper.loop(Looper.java:123)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at java.lang.reflect.Method.invoke(Method.java:521)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at dalvik.system.NativeStart.main(Native Method)
03-13 08:13:38.292: E/AndroidRuntime(2002): Caused by: java.lang.NullPointerException
03-13 08:13:38.292: E/AndroidRuntime(2002):     at com.mypackage.ContentPage.onCreate(ContentPage.java:41)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 08:13:38.292: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 08:13:38.292: E/AndroidRuntime(2002):     ... 11 more
03-13 08:13:40.142: I/Process(2002): Sending signal. PID: 2002 SIG: 9

код Мпермана logcat

03-13 08:44:17.112: E/AndroidRuntime(2142): FATAL EXCEPTION: main
03-13 08:44:17.112: E/AndroidRuntime(2142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.ContentPage}: java.lang.ClassCastException: android.widget.TextView
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.os.Looper.loop(Looper.java:123)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at java.lang.reflect.Method.invoke(Method.java:521)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at dalvik.system.NativeStart.main(Native Method)
03-13 08:44:17.112: E/AndroidRuntime(2142): Caused by: java.lang.ClassCastException: android.widget.TextView
03-13 08:44:17.112: E/AndroidRuntime(2142):     at com.mypackage.ContentPage.onCreate(ContentPage.java:42)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 08:44:17.112: E/AndroidRuntime(2142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 08:44:17.112: E/AndroidRuntime(2142):     ... 11 more

contentpage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/content_p" >
    <TextView
    android:id="@+id/testing123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="HELLO"/>
</LinearLayout>

Ответы [ 2 ]

0 голосов
/ 13 марта 2012

попробуйте

AutoResizeTextView test;
Boolean fit = true;
@Override
public void onCreate(Bundle savedInstanceState) 
{   
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
setContentView(R.layout.content_page); //Set ContentView Before accessing R.id.testing123 parent layout    
test =  new AutoResizeTextView(this);
if (winMan != null)
{
    int orientationS = winMan.getDefaultDisplay().getOrientation();   
    if (orientationS == 0) 
    {
    //setContentView(R.layout.content_page);  maybe here
        test = (AutoResizeTextView)findViewById(R.id.testing123);
        test.setTextColor(R.color.red); 
    test.resizeText();
        // Portrait

        //addListenerOnButton();
    }
0 голосов
/ 13 марта 2012

обновить код:

    AutoResizeTextView test;
    Boolean fit = true;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {   
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.content_page);  maybe here
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
        setContentView(R.layout.content_page); //Set ContentView Before accessing R.id.testing123 parent layout    
        test =  new AutoResizeTextView(this);
        if (winMan != null)
        {
            int orientationS = winMan.getDefaultDisplay().getOrientation();   
            if (orientationS == 0) 
            {
            //setContentView(R.layout.content_page);  maybe here
                test = (AutoResizeTextView)findViewById(R.id.testing123);
                test.setTextColor(R.color.red); 
            test.resizeText();
                // Portrait
                setContentView(R.layout.content_page);          
                //addListenerOnButton();
            }
...