ActionBar.setTitle()在模拟器上运行但在设备上崩溃了?

时间:2017-08-11 16:39:39

标签: android

在Android模拟器上运行时,我的应用程序正常运行,但在Android设备上运行时会崩溃。我看到它因ActionBar.setTitle()而崩溃,但错误是什么?发布主要活动,Logcat错误和构建Gradle的代码。

package com.example.mher.citygo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    String[] items;
    private static final int REQUEST_CALL=1;
    ArrayList<String> listItems;
    ArrayAdapter<String> adapter;
    ListView listView;
    EditText editText;
    Button bregister;
    private Toolbar toolbar;
    ImageView help;

                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    toolbar=(Toolbar) findViewById(R.id.my_toolbar);
                    setSupportActionBar(toolbar);
                    getSupportActionBar().setTitle(null);



                ArrayList<ItemData> list=new ArrayList<>();
                list.add(new ItemData("Eng",R.drawable.usaflag));
                list.add(new ItemData("Arm",R.drawable.armeniaflag));
                list.add(new ItemData("Ru",R.drawable.russiaflag));
                list.add(new ItemData("Per",R.drawable.iranflag));

                final Button SearchARid=(Button) findViewById(R.id.bSearchRide);
                Button LoginDriver=(Button) findViewById(R.id.bLogin_As_A_Driver);
                bregister=(Button) findViewById(R.id.bRegister);
                help=(ImageView) findViewById(R.id.help);

                help.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(MainActivity.this,ActivityHelp.class);
                        start

Activity(intent);
                    overridePendingTransition( R.anim.slide_in_up,R.anim.stay);
                }
            });




            bregister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent SignUp=new Intent(MainActivity.this,RegisterActivity.class);
                    MainActivity.this.startActivity(SignUp);

                }
            });

            LoginDriver.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent DriverLogin=new Intent(MainActivity.this,LoginActivity.class);
                    MainActivity.this.startActivity(DriverLogin);
                }
            });

            SearchARid.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent SearchRide=new Intent(MainActivity.this, SearchRide.class);
                    MainActivity.this.startActivity(SearchRide);
                }
            });
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater menuInflater=getMenuInflater();
            menuInflater.inflate(R.menu.main_menu,menu);

            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int res_id=item.getItemId();
            if(res_id==R.id.actionSettings)
            {
                Intent intent=new Intent(MainActivity.this,SettingsActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.right_to_left,R.anim.stay);

            }

            return super.onOptionsItemSelected(item);
        }


    }

这是我的Gradle(Mudule App):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.mher.citygo"
        minSdkVersion 22
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-database:9.2.1'
    compile 'com.google.firebase:firebase-auth:9.2.1'
    compile 'com.intuit.sdp:sdp-android:1.0.4'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

这是我的LogCat错误

08-11 20:27:00.404 21175-21175/com.example.mher.citygo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.mher.citygo, PID: 21175
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mher.citygo/com.example.mher.citygo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:223)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
                                                                                 **

    **at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:158)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)**

**
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                                 at com.example.mher.citygo.MainActivity.onCreate(MainActivity.java:39)
                                                                                 at android.app.Activity.performCreate(Activity.java:6877)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352) 
                                                                                 at android.app.ActivityThread.access$1100(ActivityThread.java:223) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:158) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7231) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

2 个答案:

答案 0 :(得分:0)

答案很简单。您在设备和模拟器上运行不同版本的Android,并且您的设备未使用ActionBar / Toolbar作为主题,因此它也无法设置标题。 我不太确定,但因为我没有看到你的完整项目(有布局,样式等)。您首先设置SupportActionBar,findViewById显然返回null一次,这是肯定的,或者您正在运行不同版本的应用程序。因此,由于某些原因,您的物理设备上没有工具栏。

答案 1 :(得分:0)

尝试关注而不是getSupportActionBar().setTitle(null)

Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
TextView yourTextView = (TextView) f.get(toolbar);
yourTextView.setTitle("");