java android.view.View.setVisibility(int) 使新活动崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35611546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.view.View.setVisibility(int) crashes the new activity
提问by brettbrdls
So recently I'm trying to implement a FAB that will expand into a new activity but it crashes after the FAB was pressed. Moreover from this question where I followed the steps on implementing it: FloatingActionButton expand into a new activity
所以最近我试图实现一个 FAB,它将扩展到一个新的活动,但在按下 FAB 后它崩溃了。此外,从这个问题,我遵循了实现它的步骤:FloatingActionButton expand into a new Activity
Logcat:
日志猫:
Process: com.example.jovie.canteen, PID: 6953
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
at com.example.jovie.canteen.Home.run(Home.java:105)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Home.java
主页.java
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
CoordinatorLayout homeLayout;
private GoogleApiClient client;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private RevealLayout mRevealLayout;
View mViewToReveal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
homeLayout = (CoordinatorLayout) findViewById(R.id.CoordinatorLayout);
setSupportActionBar(toolbar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
mRevealLayout = (RevealLayout) findViewById(R.id.reveal_layout);
final View mRevealView=(View)
findViewById(R.id.reveal_view);
final FloatingActionButton mFab = (FloatingActionButton) findViewById(R.id.fab);
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mFab.setClickable(false); // Avoid naughty guys clicking FAB again and again...
int[] location = new int[2];
mFab.getLocationOnScreen(location);
location[0] += mFab.getWidth() / 2;
location[1] += mFab.getHeight() / 2;
final Intent intent = new Intent(Home.this, SearchActivity.class);
mRevealView.setVisibility(View.VISIBLE);
mRevealLayout.setVisibility(View.VISIBLE);
mRevealLayout.show(location[0], location[1]); // Expand from center of FAB. Actually, it just plays reveal animation.
mFab.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(intent);
/**
* Without using R.anim.hold, the screen will flash because of transition
* of Activities.
*/
overridePendingTransition(0, R.anim.hold);
}
}, 600); // 600 is default duration of reveal animation in RevealLayout
mFab.postDelayed(new Runnable() {
@Override
public void run() {
mFab.setClickable(true);
mRevealLayout.setVisibility(View.INVISIBLE);
mViewToReveal.setVisibility(View.INVISIBLE);
}
}, 960); // Or some numbers larger than 600.
}
});
client = new GoogleApiClient.Builder(Home.this).addApi(AppIndex.API).build();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
activity_home.xml
活动主页.xml
<com.example.jovie.canteen.RevealLayout
android:id="@+id/reveal_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<View
android:id="@+id/reveal_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>
</com.example.jovie.canteen.RevealLayout>
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer"
android:background="#ffffff" />
However the null on android.view.View.setVisibility(int)
is pointing at mViewToReveal.setVisibility(View.INVISIBLE);
and not sure where exactly I need to look for, please help.
但是,空值android.view.View.setVisibility(int)
指向mViewToReveal.setVisibility(View.INVISIBLE);
并不确定我到底需要在哪里寻找,请帮忙。
回答by Rohit Arya
Because you never instantiated mViewToReveal
因为你从未实例化 mViewToReveal
add this in onCreate
添加这个 onCreate
mViewToReveal = findViewById(R.id.reveal_view);
mViewToReveal = findViewById(R.id.reveal_view);
回答by king
It's because you never instantiated mViewToReveal
.
这是因为您从未实例化mViewToReveal
.
回答by Geralt_Encore
mViewToReveal
hasn't been initialized.
mViewToReveal
尚未初始化。
回答by Rohit Patil
Because you did not pass the reference of any view to mViewToReveal that's why it's null and throwing error "Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference", need to pass reference to mViewToReveal before doing any activity on it
因为您没有将任何视图的引用传递给 mViewToReveal 这就是为什么它为空并抛出错误“尝试在空对象引用上调用虚方法 'void android.view.View.setVisibility(int)'”,需要将引用传递给在对其进行任何活动之前 mViewToReveal
mViewToReveal =(View) findViewById(R.id.<id_of_ViewToReveal>);