因为,我是android新手。我不了解调试我的应用程序的方法。 我尝试打开它时,我的应用程序停止工作。我正在尝试创建侧边栏或导航抽屉。
这是我的布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The first child in the layout is for the main Activity UI-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Holy Operating Systems, Batdroid!"
android:textSize="24sp"
android:gravity="center"
android:layout_marginTop="100dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/batdroid"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<!-- Side navigation drawer UI -->
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
这是mainActivity代码:
package ems.pixelapp.com.ems;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@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;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我得到的错误,因为我的应用已停止工作
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: FATAL EXCEPTION: main
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{ems.pixelapp.com.ems/ems.pixelapp.com.ems.MainActivity}: java.lang.NullPointerException
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:151)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.os.Looper.loop(Looper.java:155)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5559)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: Caused by: java.lang.NullPointerException
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at ems.pixelapp.com.ems.MainActivity.onCreate(MainActivity.java:45)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5066)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:151)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.os.Looper.loop(Looper.java:155)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5559)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
01-13 16:24:51.807 11270-11270/ems.pixelapp.com.ems E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
正如@Roshan指出的那样,我使用的主题不支持getSupportActionBar()。
我将主题改为Theme.AppCompat,它就像魅力一样。