Null Pointer Exception with the actionBar - java

I have implemented swipe view but I am getting a Null Poitner Exception and the logcat says:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.app.ActionBar$Tab android.app.ActionBar.newTab()' on a
null object reference at
com.example.android.mainactivityiit.MainActivity.onCreate(MainActivity.java:37)
I have been through numerous answers for the same question but none solved my issue. I changed the getActionBar to getSupportActionBar but still the error stays. Pleaase help me at the earliest.
here is my code:(Edited latest code)
package com.example.android.mainactivityiit;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
this is tabspageadapter:
package com.example.android.mainactivityiit;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
I have implemented this activity using intent so the manifest file is for the other activity from which I am calling the swipe view activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mainactivityiit" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivityiit"
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=".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>

Change following line
actionBar = getActionBar();
to
actionBar = getSupportActionBar();
UPDATE :
you have to import android.support.v7.app.actionBar not android.app.actionBar.
hope this will helps you.

Related

App crashing after using intent to open a new activity after login

I have been trying to get my app to redirect to a new activity after login, I have tried almost everything to no avail. I have also checked all related answers here on the forums but nothing seems to work. Any help would be greatly appreciated.
The Login Activity Code:
#Override
public void onSuccess(#NonNull final Credentials credentials) {
Intent i = new Intent(MainActivity.this, testactivity.class);
startActivity(i);
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.auth0.samples">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="#string/auth0_domain"
android:pathPrefix="/android/com.auth0.samples/callback"
android:scheme="demo" />
</intent-filter>
</activity>
<activity android:name="materialtabs.activity.SimpleTabsActivity" />
<activity android:name=".testactivity"></activity>
</application>
</manifest>
Test.java:
package com.auth0.samples;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import com.auth0.samples.fragments.OneFragment;
import com.auth0.samples.fragments.ThreeFragment;
import com.auth0.samples.fragments.TwoFragment;
import java.util.ArrayList;
import java.util.List;
public class test extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Notices");
adapter.addFragment(new TwoFragment(), "Timetable");
adapter.addFragment(new ThreeFragment(), "Homework");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Upon further trouble shooting I have noticed that the app will redirect to a blank activity so there must be something that is being called in test.java that is messing things up.
Welcome, a few tips first: post the error logs and more related code. In this example, under what context is the onSuccess(...) called.
Now to answer your question, I think it fails because your intent code is in onSuccess call. Have you tried debugging by moving the startActivity to outside of the onSuccess? Let's try the easiest test, simply move that intent code to the onCreate of your MainActivity. This should make it so that your mainactivity immediately go to testactivity. If your app does not do this, it means something is wrong in the testactivity.java file. If it does, it means your onsuccess() is problem.
By doing simple debugging such as above, you can easily isolate the real issue, which leads to solving the problem. Start debugging in small incremental steps! Also if you haven't already , refer here for more details on using intents to start activity.
Edit: thanks I see the new code, so your test class is called test.java instead of testactivity.java? Then in your intent, the target should be test.class.

Error creating tabs

What does Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference meant? I tried many solution but no one can solve my problem.
Tab.java
package com.example.project.project;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class Tab extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tabs
actionBar.addTab(actionBar.newTab().setText("Information").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Force").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Work Details").setTabListener(tabListener));
}
}
TabPagerAdapter.java
package com.example.project.project;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Information();
case 1:
return new WorkDetailsTable();
}
return null ;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3; //No of Tabs you can give your number of tabs
}
}
Tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project.project" >
<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>
<activity
android:name=".Tab">
</activity>
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
</application>
</manifest>
Error
10-04 11:11:07.492 2146-2146/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 2146
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project.project/com.example.project.project.Tab}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
And in my Mainfest.xml, the compiler said
<activity android:name=".WorkDetailsTable" android:screenOrientation="landscape" />
<activity android:name="Information"/>
is not assignable to android.app....
Try replacing
actionBar = getActionBar();
with
actionBar = ((AppCompatActivity) activity).getSupportActionBar();
where
Activity activity = (Activity) this;
Try to replace this lines:
actionBar = getActionBar();
With:
actionBar = ((AppCompatActivity) mActivity).getSupportActionBar();
And add this on top of onCreate()
Activity mActivity = (Activity) this;
And start activity with this:
<activity
android:name=".YourActivityClassName"
android:label="YourActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="package.name.YOURACTIVITYCLASSNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This is listener:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Tab selected", 2000).show();
}
}
Refer to this: FragmentActivity vs Activity
Also remember to use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment to an android.app.Activity, as this will cause an exception to be thrown.
Actionbar has to be replaced with toolbar now, all you need to do is include a toolbar in your xml files. I used the appcompact library toolbar, for this you may have to add support library in your gradle file
compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:appcompat-v7:22.0.0'
Add follwing toolbar to xml file
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
Don't forget to inherit your activity from Appcompactactivity, you may use your own toolbar, finally access the toolbaar in activity
Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference

I have seen other soution but i think my situation is diffrent . , I am trying to implement
ActionBar with Tabs from this example ;
http://www.androidgreeve.com/2014/01/android-actionbar-navigating-with-swipeable-tabs-and-views.html
on
actionBar.setHomeButtonEnabled(false);
program throws error :
Attempt to invoke virtual method 'void
android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object
reference
here is the code
package com.example.administrator.fragmentsexample;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.example.administrator.adapter.TabsPagerAdapter;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Social", "Organizer", "Movies"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
My Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.fragmentsexample" >
<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>
I tried to replace FragmentActivity with ActioBarActivity but it is shoing deprecated API Error
i think I am missing very basic thing .. what am I missing in code ?
FragmentActivity out of support.v4 is not including a SupportActionBar, this why you get Null Reference.
Change your class definition to extend AppCompatActivity which extends FragmentActivity like so:
public class MainActivity extends AppCompatActivity implements
ActionBar.TabListener {
...
}
Change your Import of ActionBar to:
import android.support.v7.app.ActionBar
Because like the API Docs said: http://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html#getSupportActionBar()
It returns the support library version of the ActionBar impl. ;)
Attention!
Regarding Deprecated mark on SupportActionBarActivity I changed extend to AppCompatActivity.
Thats all.
Change ActionBarAtivity to AppCompatActivity

Search Dialogue not activated when onSearchRequested is called (Android)

Beginner working on an android app and I'm trying to start a searchable activity when the onSearchRequested method is called after a button push in the main activity. Currently, when the button is pressed, nothing happens.
Here's MainActivity.Java:
package com.example.activity2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onSearchRequested();
}
});
}
/** Called when the user clicks the Send button */
// public void sendMessage(View view)
// {
// }
#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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here's the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity2"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/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.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.FLAG_ACTIVITY_NEW_TASK" />
<meta-data
android:name="android.app.default_searchable"
android:value="com.example.activity2.SearchActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.authorwjf.youtubeapi.MainActivity"
android:label="#string/title_activity_activity2" >
</activity>
<activity android:name=".SearchActivity" >
android:launchMode="singleTop" >
<intent-filter>
<action android:name="com.example.activity2.SEARCHACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="#string/title_activity_other" >
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchListView"
android:label="#string/title_activity_search_list_view" >
</activity>
<activity
android:name=".SearchYoutube"
android:label="#string/title_activity_search_youtube" >
</activity>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</application>
</manifest>
SearchActivity.Java
package com.example.activity2;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.api.services.youtube.model.SearchResult;
public class SearchActivity extends Activity {
ListView lst;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_activity);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
ArrayList<SearchResult> results = new ArrayList<SearchResult>();
Iterator<SearchResult> it = results.iterator();
SearchYoutube.prettyPrint(it, query);
lst = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, SearchYoutube.ytstuff);
lst.setAdapter(adapter);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In your searchable.xml file in the xml folder, the label cannot be hardcoded. It should be like #string/name or search dialogue wont show.
You need to implement your search activity.
public class SearchableActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SEARCH", "HERE");
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// call the appropriate detail activity
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
Toast.makeText(getApplicationContext(), queryStr, Toast.LENGTH_LONG).show();
}
}
And specify the meta-data in your AndroidManifest.xml
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchableActivity" />
<activity
android:name=".SearchableActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>

Android App crash with two activity

I follow this tutorial and i follow all the steps but when i start the app on my device it says it crashed.So i want to make the MainActivity show first and then if i click a Action bar menu, it will load the second activity (upload.java) ant it will show the upload_layout.I'm training making app for android so i'm not good to program ;)
HERE MY CODE:
MainActivity.java
package com.example.tosseapp.app;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.*;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void tosse1(View v) {
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.toxxe);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse2(View v) {
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tozze);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse3(View v) {
Button two = (Button)this.findViewById(R.id.button3);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossesei);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse4(View v) {
Button two = (Button)this.findViewById(R.id.button4);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.todde);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse5(View v) {
Button two = (Button)this.findViewById(R.id.button5);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossequattro);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void tosse6(View v) {
Button two = (Button)this.findViewById(R.id.button6);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tossecinque);
two.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
public void upload(View view) {
Intent intent = new Intent(this, upload.class);
startActivity(intent);
}
}
upload.java:
package com.example.tosseapp.app;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class upload extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.upload, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tosseapp.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity
android:name="com.example.tosseapp.app.upload"
android:label="#string/app_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml (menu)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tosseapp.app.MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:onClick="upload"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You need to declare your MainActivity in your AndroidManifest.xml
<application
android:allowBackup="true"
<!-- other things -->
<activity
android:name="com.example.tosseapp.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- other activities, services, receivers & close application -->
You may need to add both your MainActivity and upload.java into your manifest, something like this:
<application
...
<activity
android:name="com.example.tosseapp.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tosseapp.app.upload"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.tosseapp.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.tosseapp.app.MainActivity" />
</activity>
</application>

Categories