按钮可能会产生空指针异常(Android的工作室) [英] Button may produce null pointer exception (Android Studio)
问题描述
新到Android工作室,我想我在做没有问题,但是遇到了一个错误,昨晚,我只是不能似乎不顾一切我最好的谷歌搜索的努力来解决。在我的活动之一,A键'可能会产生显示java.lang.NullPointerException',但它未能保持每次被推时间。这可能只是作为一个排序线code在错误的地方等这样简单的事情,但我很新的到Android的工作室我真的不知道我要去哪里错了。
公共类searchPage扩展AppCompatActivity { 私人GoogleApiClient客户端;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_search_page);
工具条工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏); 按钮goBackButton =(按钮)findViewById(R.id.goBackButton); goBackButton.setOnClickListener(新View.OnClickListener(){
@覆盖 公共无效的onClick(视图v){ startActivity(新意图(searchPage.this,MainActivity.class));
}
});
下面是XML
<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=新按钮
机器人:ID =@ + ID / goBackButton
机器人:layout_marginTop =88dp
机器人:layout_below =@ + ID / spinner4
机器人:layout_centerHorizontal =真
/>
和清单文件
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.onein.mobilefinalapp> <应用
机器人:allowBackup =真
机器人:图标=@的mipmap / ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:supportsRtl =真
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme.NoActionBar>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名字=。searchPage
机器人:标签=@字符串/ title_activity_search_page
机器人:主题=@风格/ AppTheme.NoActionBar/> <活动
机器人:名字=。MoviePage
机器人:标签=@字符串/ title_activity_movie_page
机器人:主题=@风格/ AppTheme.NoActionBar>< /活性GT; &所述;元数据
机器人:名字=com.google.android.gms.version
机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/>
< /用途> < /清单>
这里的activity_search_page.xml
< android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:主题=@风格/ AppTheme.AppBarOverlay> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:popupTheme =@风格/ AppTheme.PopupOverlay/> < /android.support.design.widget.AppBarLayout> <包括布局=@布局/ content_search_page/> < android.support.design.widget.FloatingActionButton
机器人:ID =@ + ID / FAB
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底部|结束
机器人:layout_margin =@扪/ fab_margin
机器人:SRC =@机器人:可绘制/ ic_dialog_email/>< /android.support.design.widget.CoordinatorLayout>
如果您需要任何其他信息,请让我知道。
任何帮助非常AP preciated!谢谢。
编辑 - 复制去除按钮,将在activity_search_page.xml
方法调用'setOnClickListener'可能会产生显示java.lang.NullPointerException少...
此检查分析方法控制和数据流报告,总是为真或假可能的条件下,前pressions其值是静态被证明是恒定的,而且可能会导致空性合同违约的情况。
块引用>这仅仅是一个警告,因为
findViewById
将返回空
如果给定ID可以在布局中找到。您可以放心地忽略它,如果你知道id是您正在使用的布局,或者你可以用一个断言忽略它。 (也可能是一个
@Nullable
注释)视图V = findViewById(R.id.someId);
断言V =空!;
v.setOnClickListener(...);new to Android Studio, and I thought I was doing okay, but ran into an error last night that I just can't seem to fix despite all my best googling efforts. A button on one of my activities 'may produce java.lang.NullPointerException', except it keeps failing every time it's pushed. It may just be something as simple as ordering a line of code in the wrong place etc, but I'm so new to Android studio I don't really know where I'm going wrong.
public class searchPage extends AppCompatActivity { private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_page); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Button goBackButton = (Button) findViewById(R.id.goBackButton); goBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(searchPage.this, MainActivity.class)); } });
Here's the XML
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/goBackButton" android:layout_marginTop="88dp" android:layout_below="@+id/spinner4" android:layout_centerHorizontal="true" />
And the Manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.onein.mobilefinalapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".searchPage" android:label="@string/title_activity_search_page" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".MoviePage" android:label="@string/title_activity_movie_page" android:theme="@style/AppTheme.NoActionBar"></activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application> </manifest>
Here's the activity_search_page.xml
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_search_page" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout>
If you need any other information, please let me know.
Any help greatly appreciated! Thanks.
EDIT - removed duplicate button, and adding in activity_search_page.xml
解决方案Method invocation 'setOnClickListener' may produce 'java.lang.NullPointerException' less...
This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.
This is only a warning because
findViewById
will returnnull
if the given id can be found in the layout.You can safely ignore it if you know that the id is in the layout you are using, or you can ignore it with an assertion. (And possibly an
@Nullable
annotation)View v = findViewById(R.id.someId); assert v != null; v.setOnClickListener(...);
这篇关于按钮可能会产生空指针异常(Android的工作室)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!