Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – Button可能会产生空指针异常(Android Studio)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Android studio的新手,我认为我做得很好,但昨晚遇到了一个错误,尽管谷歌搜索力度最大,但我似乎无法修复.我的一个活动上的一个按钮’可能会产生java.lang.NullPointerException’,除了它每次推送都会失败.它可能就像在错误的地方订购一行代码一样简单,但我对Android工作室这么新,我真的不知道我哪里出错了.
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"
    />

和清单文件

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://scheR_472_11845@as.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

<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>

如果您需要任何其他信息,请告诉我.

任何帮助非常感谢!谢谢.

编辑 – 删除重复按钮,并添加activity_search_page.xml

解决方法

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

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

View v = findViewById(R.id@L_387_15@mEID);
assert v != null;
v.setOnClickListener(...);

大佬总结

以上是大佬教程为你收集整理的java – Button可能会产生空指针异常(Android Studio)全部内容,希望文章能够帮你解决java – Button可能会产生空指针异常(Android Studio)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。