Sunday, 1 February 2015

android app crashing after adding navigation drawer



i am working on this android app.In this app, the "StartingActivity.java" is automatically getting changed to "HomeActivity.java" which contains the Navigation Drawer. But while changing the activity,the app crashes.Please help.. Here is the code


StartingActivity.java



package com.example.abhi.mymanager;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class StartingActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting);
// for sending it to another activity after a time
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent i=new Intent(StartingActivity.this,HomeActivity.class);
startActivity(i);
}
}
},5000);
}

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


HomeActivity.java



package com.example.abhi.mymanager;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
* Created by abhi on 29-01-2015.
*/
public class HomeActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

private DrawerLayout drawerLayout;
private ListView listView;
private String[] options;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
listView=(ListView)findViewById(R.id.left_drawer);
listView.setAdapter(new ArrayAdapter<> (this,android.R.layout.simple_list_item_1,options));
listView.setOnItemClickListener(this);



}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,options[position],Toast.LENGTH_LONG).show();
getSupportActionBar().setTitle(options[position]);
}
}


activity_starting.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" 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=".StartingActivity"
android:background="#ffb6a6ff">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Manager"
android:id="@+id/textView"
android:layout_marginTop="65dp"
android:textSize="@dimen/abc_action_bar_default_height_material"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ff2312ff" />
</RelativeLayout>


home_layout.xml



<android.support.v4.widget.DrawerLayout
xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- the main content view-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- the navigation drawer-->
<ListView
android:id="@+id/left_drawer"
android:entries="@array/Options"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@android:color/holo_purple"

/>
</android.support.v4.widget.DrawerLayout>


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.example.abhi.mymanager" >

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

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity"
android:label="@string/app_name"
>
</activity>

</application>

</manifest>


strings.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">My Manager</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="Options">
<item name="MyBoard">My Board</item>
<item name="CreateBoard">Create Board</item>
</string-array>


</resources>

No comments:

Post a Comment