android为什么我无法将项目主题更改为noactionbar

时间:2015-04-23 14:59:50

标签: android themes

<style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

    </style>

我的项目minimun sdk是API16。我正在按照教程https://www.youtube.com/watch?v=pMO8EVkhJO8将主题设置为Theme.AppCompat.Light.NoActionBar。如果我运行应用程序,null异常显示...我该怎么办。 gradle文件有问题吗?

04-23 15:03:57.589    2045-2045/com.it.fyp.tab E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.it.fyp.tab/com.it.fyp.tab.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.it.fyp.tab.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.it.fyp.tab" >


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{

    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] planets = {"A", "B", "C", "D", "E"};
    private ActionBarDrawerToggle drawerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.drawerList);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets));
        listView.setOnItemClickListener(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerListener = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer){
            @Override
            public void onDrawerOpened(View drawerView) {
                Toast.makeText(MainActivity.this, "Drawer Opened", Toast.LENGTH_SHORT).show();
                invalidateOptionsMenu();
                syncState();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Toast.makeText(MainActivity.this, "Drawer Closed", Toast.LENGTH_SHORT).show();
                invalidateOptionsMenu();
                syncState();
            }
        };
        drawerLayout.setDrawerListener(drawerListener);
        if (toolbar != null){
            setSupportActionBar(toolbar);
        }else{
            Log.i("NULL TOOLBAR", "YES");
        }
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
        selectItem(position);
    }

    private void selectItem(int position) {
        listView.setItemChecked(position, true);
        setTitle(planets[position]);
    }

    public void setTitle(String title) {
        getSupportActionBar().setTitle(title);
    }
}

2 个答案:

答案 0 :(得分:0)

创建如下的主题 -

uses
  Menus;


procedure TForm17.CreateMainMenu;
var
  MainMenu: TMainMenu;
  TopMenuItem, MenuItem, SubMenuItem: TMenuItem;
  i, j, k: Integer;
begin
  MainMenu := TMainMenu.Create(Self);

  for i := 0 to 5 do
  begin
    TopMenuItem := TMenuItem.Create(MainMenu);
    TopMenuItem.Caption := 'Top menu item #' + i.ToString;
    TopMenuItem.OnClick := MenuItemClick;
    MainMenu.Items.Add(TopMenuItem);
     TopMenuItem := MainMenu.Items[i];

    for j := 0 to 10 do
    begin
      MenuItem := TMenuItem.Create(MainMenu);
      MenuItem.Caption := 'Menu item #' + j.ToString;
      MenuItem.OnClick := MenuItemClick;
      TopMenuItem.Add(MenuItem);
      MenuItem := TopMenuItem.Items[j];

      for k := 0 to 10 do
      begin
        SubMenuItem := TMenuItem.Create(MainMenu);
        SubMenuItem.Caption := 'Sub Menu item #' + k.ToString;
        SubMenuItem.OnClick := MenuItemClick;
        MenuItem.Add(SubMenuItem);
        SubMenuItem := MenuItem.Items[k];
      end;
    end;
  end;
end;


procedure TForm17.FormCreate(Sender: TObject);
begin
  CreateMainMenu;
end;

procedure TForm17.MenuItemClick(Sender: TObject);
begin

end;

答案 1 :(得分:0)

我认为使用父级处理自定义主题的最简单方法是直接在Manifest中声明主题:

android:theme="@android:style/Theme.AppCompat.Light.NoActionBar"

然后在您的单独样式中,XML将该主题作为父级:

<style name="AppTheme" parent="android:Theme.AppCompat.Light.NoActionBar"
   <item name=..../>
    ....
</style>

然后在您的Manifest中的特定活动片段中,将主题设置为您的新主题:

<activity>
   android:theme="@style/AppTheme" >
</activity>