无法访问包含的工具栏的内容并获取 getSupportActionBar() 的空指针异常
unable to access the contents of included toolbar and getting nullpointer exception to getSupportActionBar()
1] 我无法访问工具栏的内容。我已经创建了 toolbar.xml 布局,并将其添加到像这样的所需布局中。
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bitcoin_logo" />
<TextView
android:id="@+id/toolbar_txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dp"
android:text="gfdssfgfg"
android:textColor="@color/colorToolbarText"
android:textSize="@dimen/default_text_size"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
mainlayout.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="match_parent"
android:background="@drawable/side_nav_bar">
<include
android:id="@+id/appbar"
layout="@layout/toolbar"></include>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_buy"
android:layout_below="@+id/appbar"
android:fillViewport="true"
android:fitsSystemWindows="true">
<!-- stuff-->
</ScrollView>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_buy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="#2e7826"
android:gravity="center"
android:hint="Ask"
android:textColor="@color/colorWhite"
android:textColorHint="@color/colorWhite"
android:textSize="@dimen/BTC_title_size" />
</RelativeLayout>
当我尝试访问工具栏的内容时,出现错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.kiran.example.ebitcoin.activity.ActivityAsk.onCreate(ActivityAsk.java:26)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
2] 我想在操作栏上添加后退按钮,所以我设置
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
然后抛出如下异常
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at com.kiran.example.ebitcoin.activity.ActivityAsk.onCreate(ActivityAsk.java:28)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
我的.java代码是
public class ActivityAsk extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar)findViewById(R.id.appbar);
setSupportActionBar(toolbar);
TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
txt.setText("text"); //exception 1
getSupportActionBar().setDisplayShowHomeEnabled(true);//exception 2
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//exception 2
setContentView(R.layout.activity_ask);
}
}
在初始化 TextView 和工具栏之前设置行 setContentView(R.layout.activity_ask);
尝试下面的代码
public class ActivityAsk extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask);//change here
toolbar = (Toolbar)findViewById(R.id.appbar);
setSupportActionBar(toolbar);
TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
txt.setText("text"); //exception 1
getSupportActionBar().setDisplayShowHomeEnabled(true);//exception 2
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//exception 2
}
}
1] 我无法访问工具栏的内容。我已经创建了 toolbar.xml 布局,并将其添加到像这样的所需布局中。
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bitcoin_logo" />
<TextView
android:id="@+id/toolbar_txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="34dp"
android:text="gfdssfgfg"
android:textColor="@color/colorToolbarText"
android:textSize="@dimen/default_text_size"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
mainlayout.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="match_parent"
android:background="@drawable/side_nav_bar">
<include
android:id="@+id/appbar"
layout="@layout/toolbar"></include>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_buy"
android:layout_below="@+id/appbar"
android:fillViewport="true"
android:fitsSystemWindows="true">
<!-- stuff-->
</ScrollView>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_buy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:background="#2e7826"
android:gravity="center"
android:hint="Ask"
android:textColor="@color/colorWhite"
android:textColorHint="@color/colorWhite"
android:textSize="@dimen/BTC_title_size" />
</RelativeLayout>
当我尝试访问工具栏的内容时,出现错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.kiran.example.ebitcoin.activity.ActivityAsk.onCreate(ActivityAsk.java:26)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
2] 我想在操作栏上添加后退按钮,所以我设置
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
然后抛出如下异常
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at com.kiran.example.ebitcoin.activity.ActivityAsk.onCreate(ActivityAsk.java:28)
at android.app.Activity.performCreate(Activity.java:6977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
我的.java代码是
public class ActivityAsk extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar)findViewById(R.id.appbar);
setSupportActionBar(toolbar);
TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
txt.setText("text"); //exception 1
getSupportActionBar().setDisplayShowHomeEnabled(true);//exception 2
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//exception 2
setContentView(R.layout.activity_ask);
}
}
在初始化 TextView 和工具栏之前设置行 setContentView(R.layout.activity_ask);
尝试下面的代码
public class ActivityAsk extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask);//change here
toolbar = (Toolbar)findViewById(R.id.appbar);
setSupportActionBar(toolbar);
TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
txt.setText("text"); //exception 1
getSupportActionBar().setDisplayShowHomeEnabled(true);//exception 2
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//exception 2
}
}