我正在使用此代码来实现自定义操作栏,但应用程序崩溃了。我正在使用布局充气器来返回自定义布局的视图,然后将其应用到操作栏。
这是custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#00897B" >
<Button
android:layout_width="190dp"
android:layout_height="wrap_content"
android:text="Register an event"
android:textColor="#ffffff"
android:background="#4db6ac"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:src="@drawable/refr"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="32dp" />
</RelativeLayout>
这是MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar=getActionBar();
LayoutInflater l=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View v1=l.inflate(R.layout.custom,null);
actionBar.setCustomView(v1);
}
}
以下是应用崩溃时的错误:
Process: com.khurana.nikhil.navigationdrawer, PID: 2255
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.khurana.nikhil.navigationdrawer/com.khurana.nikhil.navigationdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setCustomView(android.view.View)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setCustomView(android.view.View)' on a null object reference
at com.khurana.nikhil.navigationdrawer.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
答案 0 :(得分:0)
你输了
super.onCreate(savedInstanceState);
setContentView(R.layout.someView);
尝试使用getSupportActionBar()而不是getActionBar()。
如果这没有帮助,请尝试更改样式中的应用主题,也许您使用NoActionBar主题
<强>更新强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
View v1 = getLayoutInflater().inflate(R.layout.custom, null);
actionBar.setCustomView(v1);
}
}
答案 1 :(得分:0)
您如何尝试将主题设置为支持ActionBar的主题,例如Theme.Holo.Light?
此外,在您的活动中,您可以尝试getActionBar().show();
答案 2 :(得分:0)
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class Principale extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ecran_d_acceuil);
}
public boolean onCreateOptionMenu(Menu menu) {
getMenuInflater().inflate(R.menu.principale, menu);
return true;
}
}
试试这个。