按钮可能会产生空指针异常(Android Studio) [英] Button may produce null pointer exception (Android Studio)

查看:42
本文介绍了按钮可能会产生空指针异常(Android Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 的新手,我认为我做得很好,但昨晚遇到了一个错误,尽管我尽了最大努力,但我似乎无法解决这个错误.我的一项活动上的按钮可能会产生 java.lang.NullPointerException",但每次按下时它都会失败.它可能只是在错误的地方订购一行代码等简单的事情,但我对 Android Studio 如此陌生,我真的不知道我哪里出错了.

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));
        }
     });

这里是 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"
    />

和 Manifest 文件

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>

这是activity_search_page.xml

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 - 删除重复按钮,并添加到 activity_search_page.xml

EDIT - removed duplicate button, and adding in activity_search_page.xml

推荐答案

方法调用 'setOnClickListener' 可能会减少 'java.lang.NullPointerException'...

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.

这只是一个警告,因为如果在布局中找不到给定的 id,findViewById 将返回 null.

This is only a warning because findViewById will return null if the given id cannot be found in the layout.

如果您确定 id 在您使用的布局中,您可以放心地忽略它,或者您可以使用断言(可能还有 @Nullable 注释)忽略它.

You can safely ignore it if you know for sure 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 Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆