setSupportActionBar没有appcompatactivity

时间:2016-04-20 05:47:00

标签: android

我遇到了setSupportActionBar的问题 我使用extend activity和setSupportActionBar的活动需要AppCompatActivity,

但是当我将活动扩展到appcompatactivity时,我的应用程序很快停止了:

此处代码和日志

MainList.java

    package com.felix.tgp;

/**
 * Created by Felix on 4/15/2016.
 */

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainList extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    // Log tag
    private static final String TAG = MainList.class.getSimpleName();

    // Movies json url
    private static final String url = "http://....";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;

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

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

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
/*
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position) {

                ItemClicked item = adapter.getItemAtPosition(position);

                Intent intent = new Intent(MainList.this, destinationActivity.class);
                //based on item add info to intent
                startActivity(intent);

            }


        });*/

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();


        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("name"));
                                movie.setThumbnailUrl(obj.getString("images1"));
                                //movie.setDescribe(obj.getString("describe"));
                                //movie.setRating(((Number) obj.get("rating"))
                                //        .doubleValue());
                                movie.setYear(obj.getInt("id"));
                                movie.setTipe(obj.getString("tipe"));

                                /*// Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);*/

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


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

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_calculator) {
            // Handle the camera action
            Intent calculator = new Intent(MainList.this, Calculator.class);
            startActivity(calculator);
        } else if (id == R.id.nav_chest) {
            Intent List = new Intent(MainList.this, MainList.class);
            startActivity(List);
        } else if (id == R.id.nav_back) {

        } else if (id == R.id.nav_shoulder) {

        } else if (id == R.id.nav_arm) {

        } else if (id == R.id.nav_abdominal) {

        } else if (id == R.id.nav_leg) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Activity_list.xml

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainList" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

listrow.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >

    <!-- Thumbnail Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />

    <!-- Movie Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <!-- Rating -->
    <TextView
        android:id="@+id/describe"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/rating" />

    <!-- Genre -->
    <TextView
        android:id="@+id/tipe"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/describe"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- Release Year -->
    <TextView
        android:id="@+id/releaseYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/year"
        android:textSize="@dimen/year" />

</RelativeLayout>

style.xml

    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#FF39A3EB</item>
        <item name="colorPrimaryDark">#FF185896</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


</resources>

的Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.felix.tgp" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="com.felix.tgp.AppController">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Calculator"
            android:label="Body Mass Index Calculator"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name="com.felix.tgp.MainList"
            android:label="Chest Workout"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
        </activity>
    </application>

</manifest>

日志

    04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime: FATAL EXCEPTION: main
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime: Process: com.felix.tgp, PID: 32173
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.felix.tgp/com.felix.tgp.MainList}: java.lang.NullPointerException
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:  Caused by: java.lang.NullPointerException
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at com.felix.tgp.MainList.onCreate(MainList.java:51)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5231)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5017) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
04-20 13:46:31.174 32173-32173/com.felix.tgp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您尚未在Toolbar布局xml文件中定义Activity窗口小部件。你必须添加这样的东西才能摆脱NullPointerException

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:background="@color/transparent"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_marginTop="@dimen/app_bar_height"
            android:background="@color/transparent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

但这不会是你问题的结束。我看到您的代码正在寻找drawer layouts以及xml中未定义的其他内容 ......您也必须解决这个问题。