尽管设置了必要的属性,但是选取框动画在我的工具栏中不起作用。有谁知道什么是错的,以及如何解决这个问题?
不幸的是,{我的应用名称}已停止
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)
答案 0 :(得分:0)
我可能发现了什么是错的。
你调用它来获取TextView:(TextView)this.findViewById(R.id.textView1);
,但为什么要使用this
? (它指的是Activity
,因此Runtime
无法正确获取(这就是你获得此NPE的原因))
您只需使用(TextView)mToolbar.findViewById(R.id.textView1);
希望它能帮助你,Darkball60。
答案 1 :(得分:-1)
您的例外似乎是因为用于查找工具栏的ID错误。在布局中,它是tramlink_toolbar
但在活动中您使用了green_toolbar
。
Toolbar mToolbar = (Toolbar) findViewById(R.id.tramlink_toolbar);
由于您需要水平选框,因此需要为singleLine
设置true
值TextView
(目前为假)。
android:singleLine="true"