如果Activity正在使用getActionbar(),当我在android中的一个Layout中实现TextInputLayout时出错

时间:2016-12-13 09:49:09

标签: android material-design

这是我每次运行应用时加载的第一个活动。  MainActivity

public class UpdateFile extends ListActivity {

//my navigationdrawer code goes here..

oncreate method(){
getActionBar().setHomeButtonEnabled(true);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
 }
  some code goes here//
 }

}

我的浮动布局floating.xml文件在这里

<android.support.design.widget.TextInputLayout
 android:id="@+id/NameLayout"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true"
 android:layout_marginTop="10dp">
<EditText
 android:id="@+id/editname"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:layout_marginLeft="10dp"
 android:layout_marginRight="10dp"
 android:ems="10"
 android:hint="Name"/>
 </android.support.design.widget.TextInputLayout>

FloatingClass

 FloatingClass extends Activity
{
 onCreate()
 setContentView(R.layout.floating);

}

最后在gradle文件中设置依赖关系 编译'com.android.support:design:24.2.1(任何appcompat版本都有)

我收到此错误,任何人都可以指导我如何解决此问题。

  Process: com.project.app, PID: 8997                                                                          java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.project.app/com.letzkit.casenotez.update}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:154)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5276)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
                                                                     at com.project.app.update.onCreate(update.java:323)
                                                                     at android.app.Activity.performCreate(Activity.java:6041)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2285)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:154) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5276) 

先谢谢。

1 个答案:

答案 0 :(得分:0)

使用AppCompatActivity而不是ListActivity,如 -

public class UpdateFile extends AppCompatActivity

而不是

public class UpdateFile extends ListActivity

您应该通过在应用级Gradle文件中添加以下依赖项来将AppCompatActivity添加到您的应用中 -

com.android.support:appcompat-v7:25.0.1