-4

When comment actionBar.setTitle("title");, the App runs normally.

What is causing my crash?

Here is a snippet of my AppCompatActivity:

public class ActionBar2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar);

        android.app.ActionBar actionBar = getActionBar();
        actionBar.setTitle("title");
    }
}

Here is the content of AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".ActionBar"></activity>

    <activity android:name=".ActionBar2">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

I just wonder if it is something wrong with my android studio ,or something else?Thandks a lot!

Knossos
  • 15,802
  • 10
  • 54
  • 91
gaomode
  • 156
  • 1
  • 11

2 Answers2

2
public class TestActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        ActionBar bar = getSupportActionBar();
        bar.setTitle("set title here");

    }
}
Suhas Bachewar
  • 1,230
  • 7
  • 21
0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar);

    getSupportActionBar().setTitle("Set Your Title Here");
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.youricon);

     }
Abhishek
  • 1,654
  • 2
  • 18
  • 31
  • Thanks a lot! When using 'ActionBar actionBar = getActionBar()',what should I do to make it run correctly? – gaomode Jan 19 '16 at 11:08
  • In AppCompatActivity you cannot add ActionBar like this you have to use getSupportActionBar() method for it. – Abhishek Jan 19 '16 at 11:12