尽管设置了必要的属性,但字幕动画在我的工具栏中无法正常工作。有谁知道错在哪里以及如何解决此问题?


  不幸的是,{我的应用名称}已停止


XML格式

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tramlink_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/green"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <TextView
        android:id="@+id/textView1"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
        android:textColor="@android:color/white"
        android:textSize="@dimen/abc_text_size_title_material_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true">
    <requestFocus
        android:duplicateParentState="true"
        android:focusable="true"
        android:focusableInTouchMode="true" />
    </TextView>
</android.support.v7.widget.Toolbar>


爪哇

public class MainActivity extends AppCompatActivity {

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

        Toolbar mToolbar = (Toolbar) findViewById(R.id.tramlink_toolbar);
        setSupportActionBar(mToolbar);

        TextView marque = (TextView) this.findViewById(R.id.textView1);
        marque.setSelected(true);
        marque.requestFocus();
    }
}


Logcat

08-21 13:11:33.788 2935-2935/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.superbapps.myapp, PID: 2935
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.superbapps.myapp/com.superbapps.myapp.green.MainActivity}: java.lang.NullPointerException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NullPointerException
                                                     at com.superbapps.myapp.green.MainActivity.onCreate(MainActivity.java:48)
                                                     at android.app.Activity.performCreate(Activity.java:5231)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                     at dalvik.system.NativeStart.main(Native Method) 

最佳答案

我可能发现了什么问题。

您将其称为TextView:(TextView)this.findViewById(R.id.textView1);,但是为什么要使用this? (它指的是Activity,因此Runtime无法正确获取它(这就是为什么获得此NPE的原因))

您只需要使用(TextView)mToolbar.findViewById(R.id.textView1);

希望对您有帮助,Darkball60。

10-06 15:12