1

Despite setting necessary properties, the marquee animation is NOT working within my toolbar. Does anyone know what is wrong and how this issue can be fixed?

Unfortunately, {my app name} has stopped

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>

Java

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) 
wbk727
  • 8,017
  • 12
  • 61
  • 125

2 Answers2

0

I have maybe found what's wrong.

You call this to get TextView : (TextView)this.findViewById(R.id.textView1); but why do you use this ? (it refers to the Activity, so Runtime can't get it correctly (that's why you got this NPE))

You just need to use (TextView)mToolbar.findViewById(R.id.textView1);

Hope it'll help you, Darkball60.

Mesabloo
  • 337
  • 4
  • 13
-1

Your exception seems to be because of wrong Id used to find Toolbar. In layout it is tramlink_toolbar but in Activity you have used green_toolbar.

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

Since you need a horizontal marquee, you need to set singleLine valuetrue for TextView (Currently it is false).

android:singleLine="true"
Sujay
  • 3,417
  • 1
  • 23
  • 25