Android -have you declared this activity in your AndroidManifest.xml - java

I'm trying to read some data from an online server and add them dynamically in a ListView. Unfortunately it crashes with an android.content.ActivityNotFoundExceptionbut my activity is declared in the manifest so I don't understand what the problem is.
This is my java file:
public class Marker extends Activity {
// Retrieve COMMENTS from database
String myJSON;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_POI = "poi";
private static final String TAG_USERNAME ="username";
private static final String TAG_COMMENT = "comment";
JSONArray comments = null;
// ListView
private ListView lv;
private ArrayList<String> strArr;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker);
// Retrieve COMMENTS from database
getData();
}
// Retrieve COMMENTS from database
protected void showList(){
try {
lv = (ListView) findViewById(R.id.list);
strArr = new ArrayList<String>();
JSONObject jsonObj = new JSONObject(myJSON);
comments = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<comments.length();i++){
JSONObject c = comments.getJSONObject(i);
Integer id = c.getInt(TAG_ID);
String name = c.getString(TAG_POI);
String username = c.getString(TAG_USERNAME);
String comment = c.getString(TAG_COMMENT);
strArr.add("Row:" + comment);
}
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, strArr);
lv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://xxxx/comments_out.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
#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_marker_page, 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);
}
}
XML file
<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" 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="com.example.diana.track.MarkerPage">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
My Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.diana.track" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".myprofile"
android:label="#string/title_activity_myprofile" >
</activity>
<activity
android:name=".Track"
android:label="#string/title_activity_track" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".map"
android:label="#string/title_activity_map" >
</activity>
<activity
android:name=".NotificationView"
android:label="#string/title_activity_notification_view" >
</activity>
<activity
android:name=".ProximityActivity"
android:label="#string/title_activity_proximity" >
</activity>
<activity
android:name=".AddPoi"
android:label="#string/title_activity_add_poi" >
</activity>
<activity
android:name=".ShortPath"
android:label="#string/title_activity_short_path" >
</activity>
<activity
android:name=".profile"
android:label="#string/title_activity_profile" >
</activity>
<activity
android:name=".Marker"
android:label="#string/title_activity_marker" >
</activity>
</application>
</manifest>
and my logcat
07-16 18:34:27.659 8628-9083/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 3574K, 38% free 7122K/11376K, paused 57ms, total 65ms
07-16 18:34:28.059 8628-8628/com.example.diana.track I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
07-16 18:34:28.440 8628-9075/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 1605K, 35% free 7482K/11376K, paused 34ms, total 34ms
07-16 18:34:29.050 8628-9083/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 501K, 33% free 7648K/11376K, paused 33ms, total 35ms
07-16 18:34:29.050 8628-9083/com.example.diana.track I/dalvikvm-heap﹕ Grow heap (frag case) to 11.080MB for 1048592-byte allocation
07-16 18:34:30.041 8628-8628/com.example.diana.track D/AndroidRuntime﹕ Shutting down VM
07-16 18:34:30.041 8628-8628/com.example.diana.track W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415478b0)
07-16 18:34:30.061 8628-8628/com.example.diana.track E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.diana.track/com.google.android.gms.maps.model.Marker}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3390)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:3587)
at android.app.Activity.startActivity(Activity.java:3555)
at com.example.diana.track.map.onMarkerClick(map.java:765)
at com.google.android.gms.maps.GoogleMap$10.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzn$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:347)
at com.google.android.gms.maps.internal.bd.a(SourceFile:84)
at com.google.maps.api.android.lib6.d.as.b(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.c.e.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.av.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.be.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.bd.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.bt.d(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.ak.onSingleTapConfirmed(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.h.g.onSingleTapConfirmed(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.h.i.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5214)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Any idea what might be the problem?

Related

is == null while getting xml file from internet

I have the following situation: I need to get xml file from the Internet and then get lyrics from this file. Here's the url of this xml. Here's the code, which I wrote and which should do it.
private static class NetworkLyricsDownload extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=Eminem&song=Lose%20Yourself");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(getInputStream(url), "UTF-8");
boolean insideItem = false;
int eventType = xmlPullParser.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xmlPullParser.getName().equalsIgnoreCase("lyric")){
insideItem = true;
Log.i(getClass().getName(), xmlPullParser.nextText());
}
}
eventType = xmlPullParser.next();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private InputStream getInputStream(URL url){
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
But when I run the app, I have the following error:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: asus.example.com.player, PID: 14458
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalArgumentException: is == null
at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1636)
at asus.example.com.player.UploadLyricsActivity$NetworkLyricsDownload.doInBackground(UploadLyricsActivity.java:114)
at asus.example.com.player.UploadLyricsActivity$NetworkLyricsDownload.doInBackground(UploadLyricsActivity.java:86)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:764) 
I have permission in manifest file ( <uses-permission android:name="android.permission.INTERNET"/>
) but it still doesn't help. So, what's the matter and how can I solve it?
UPD
Here's my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="asus.example.com.player"
android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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"
android:networkSecurityConfig="#xml/network_security_config"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute">
<activity android:name=".SongLyricsActivity"/>
<activity android:name=".UploadLyricsActivity" />
<activity
android:name=".ListOfSongsActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService" />
</application>
</manifest>

Android Development: “thread exiting with uncaught exception”

I'm trying to create my first Android App using php but I'm having some difficulties getting started.
When I run my code I get this error log:
12-07 19:34:22.095: D/AndroidRuntime(13777): Shutting down VM
12-07 19:34:22.095: W/dalvikvm(13777): threadid=1: thread exiting with uncaught exception (group=0x42300c08)
12-07 19:34:22.095: E/AndroidRuntime(13777): FATAL EXCEPTION: main
12-07 19:34:22.095: E/AndroidRuntime(13777): Process: com.example.parents_gps, PID: 13777
12-07 19:34:22.095: E/AndroidRuntime(13777): java.lang.NullPointerException
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.example.parents_gps.MainActivity$1.onClick(MainActivity.java:42)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.view.View.performClick(View.java:4633)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.view.View$PerformClick.run(View.java:19270)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Handler.handleCallback(Handler.java:733)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.os.Looper.loop(Looper.java:146)
12-07 19:34:22.095: E/AndroidRuntime(13777): at android.app.ActivityThread.main(ActivityThread.java:5602)
12-07 19:34:22.095: E/AndroidRuntime(13777): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 19:34:22.095: E/AndroidRuntime(13777): at java.lang.reflect.Method.invoke(Method.java:515)
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-07 19:34:22.095: E/AndroidRuntime(13777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-07 19:34:22.095: E/AndroidRuntime(13777): at dalvik.system.NativeStart.main(Native Method)
I think the problem is this "thread exiting with uncaught exception" part, I have no idea what the exception could be or what's causing it.
Here is my code:
MainActivity.java: package com.example.parents_gps;
package com.example.parents_gps;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String SERVER_ADDRESS = "http://210.119.84.108/Gps";
AsyncTask<Void, Integer, Void> myTask;
ArrayList<String> identity, password;
int dataSize = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginBtn, registerBtn;
final EditText id, pw;
loginBtn = (Button) findViewById(R.id.loginBtn);
registerBtn = (Button) findViewById(R.id.registerBtn);
id = (EditText) findViewById(R.id.id);
pw = (EditText) findViewById(R.id.pw);
loginBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
&& String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {
Toast.makeText(MainActivity.this, "로그인 완료", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, LoginSuccess.class);
startActivity(intent);
} else
Toast.makeText(MainActivity.this, "ID 혹은 P/W가 올바르지 않습니다.", Toast.LENGTH_SHORT).show();
myTask = new AsyncTask<Void, Integer, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(SERVER_ADDRESS + "/search_regpar.php");
url.openStream();
identity = getXmlDataList("searchresult_p.xml", "identity");
password = getXmlDataList("searchresult_p.xml", "password");
dataSize = identity.size();
Log.i("Test", String.valueOf(identity.get(0)));
Log.i("Test2", String.valueOf(id.getText()));
for (int i = 0; i < dataSize; i++)
Log.d("size", String.valueOf(identity.get(i)));
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return null;
}
private ArrayList<String> getXmlDataList(String filename, String str) {
String rss = SERVER_ADDRESS + "/";
ArrayList<String> ret = new ArrayList<String>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL server = new URL(rss + filename);
InputStream is = server.openStream();
xpp.setInput(is, "UTF-8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals(str)) {
ret.add(xpp.nextText());
}
}
eventType = xpp.next();
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return ret;
}
};
myTask.execute();
}
});
registerBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
Here is my activity_main.xml and AndroidManifest.xml:
activity_main.xml:
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.parents_gps.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="어서오세요"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textSize="20sp" />
<EditText
android:id="#+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="xxx#mail.com"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="P/W"
android:textSize="20sp" />
<EditText
android:id="#+id/pw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Password를 입력하세요"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="로그인"
android:textSize="20sp" />
<Button
android:id="#+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="회원가입"
android:textSize="20sp" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parents_gps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".InfoTraced"
android:label="#string/app_name" >
</activity>
<activity
android:name=".InfoParents"
android:label="#string/app_name" >
</activity>
<activity
android:name=".InfoEdit"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterTraced"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterParents"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoginSuccess"
android:label="#string/app_name" >
</activity>
<activity
android:name=".MapView"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegisterActivity"
android:label="#string/app_name" >
</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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBJe2YzPGrINQM88R8yXpVisnC1sl1TpT8" />
</application>
</manifest>
It looks like you didn't initalize your 'identity' and 'password' ArrayLists. They are null when you compare it here:
if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
&& String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {

nullPointerException when accessing ActionBar

I'm getting a nullPointerException when I try to access my ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
my recipientsActivity is this
package com.ahmetyuva.ribbit;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import java.util.List;
public class RecipientsActivity extends ListActivity{
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipients);
getActionBar().setDisplayHomeAsUpEnabled(true);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
#Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> friends, ParseException e) {
if(e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
} ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getListView().getContext(),
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
}
else{
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
#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_recipients, menu);
mSendMenuItem = menu.getItem(0);
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.
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send:
return true;
}
/* int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
*/
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (l.getCheckedItemCount() > 0) {
mSendMenuItem.setVisible(true);
} else {
mSendMenuItem.setVisible(false);
}
}
}
my error log is here
04-13 11:19:05.148 1956-1956/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmetyuva.ribbit/com.ahmetyuva.ribbit.RecipientsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.ahmetyuva.ribbit.RecipientsActivity.onCreate(RecipientsActivity.java:40) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)
menu_recipients xml is here
<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.ahmetyuva.ribbit.RecipientsActivity">
<item android:id="#+id/action_send"
android:title="Send"
android:orderInCategory="100"
app:showAsAction="always"
android:visible="true"
android:icon="#drawable/ic_action_send_now"
/>
</menu>
activity_recipients xml is here
<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="com.ahmetyuva.ribbit.RecipientsActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty_recipients_list_message"
/>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ahmetyuva.ribbit" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission
android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.ahmetyuva.ribbit.permission.C2D_MESSAGE" />
<application
android:name=".RibbitApplication"
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=".LoginActivity"
android:label="#string/title_activity_login"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignUpActivity"
android:label="#string/title_activity_sign_up"
android:parentActivityName=".LoginActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".EditFriendsActivity"
android:label="#string/title_activity_edit_friends"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.ahmetyuva.ribbit" />
</intent-filter>
</receiver>
<activity
android:name=".RecipientsActivity"
android:label="#string/title_activity_recipients"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.ahmetyuva.ribbit.MainActivity" />
</activity>
</application>
</manifest>
Try to use
getSupportedActionBar() instead of getActionBar()
First of all, always do this to avoid this kind of error :
ActionBar ab = getSupportActionBar();
if (ab == null) {
Log.d("test", "ab null");
}
else {
Log.d("test", "ab not null");
ab.setDisplayHomeAsUpEnabled(true);
}
Then, what is in the logcat with the tag "test" ?
Make sure that you actually have an ActionBar, if for example, your theme is:
Theme.Light.NoActionBar, you won't have an action bar.
So, make sure your theme has and Actionbar
EDIT:
Try with the following theme:
<style name="AppTheme" parent="Theme.AppCompat">
If that doesn't work, You should consider changing to fragments, as suggested in this SO Answer
For both activities,
in your onCreate the next line after setContentView( ... )
add the following :
getActionBar().setDisplayHomeAsUpEnabled(true);
in the manifest for both activities add
android:theme="#android:style/Theme.Holo.Light"
in the tag directly after
android:screenOrientation="portrait"

Unable to start activity in inner class and activity class

I've got an android project from another developer. He can launch this project on his machine successfully, but when I try to launch it on my machine, I've got this exception when application trying load map in a fragment. The logcat with exception:
09-29 07:37:50.298 2442-2442/nz.co.tish.android.coffeetime E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nz.co.tish.android.coffeetime/nz.co.tish.android.coffeetime.fragment.MapFragment$InnerActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at nz.co.tish.android.coffeetime.fragment.MapFragment.onCreate(MapFragment.java:132)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:15848)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at nz.co.tish.android.coffeetime.fragment.MapFragmen
09-29 07:42:50.399 2442-2442/nz.co.tish.android.coffeetime I/Process﹕ Sending signal. PID: 2442 SIG: 9
Android manifest:
android:versionCode="7"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="nz.co.tish.android.coffeetime.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="nz.co.tish.android.coffeetime.permission.C2D_MESSAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="nz.co.tish.android.coffeetime.CoffeetimeApplication"
android:allowBackup="true"
android:description="#string/app_desc"
android:icon="#drawable/app_logo57"
android:label="Coffeetime TISH Limited"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDMx6IbAJEWnHuKv_yv4Pkwgx_U1mF2Qkc" />
<!--Debug: AIzaSyDMx6IbAJEWnHuKv_yv4Pkwgx_U1mF2Qkc-->
<!--Live: AIzaSyCnNpNptAzaBdtF9GOAVw-POo1gt2Tsw-Q-->
<meta-data
android:name="com.google.android.gms.versionCode"
android:value="#integer/google_play_services_version" />
<activity
android:name="nz.co.tish.android.coffeetime.MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="portrait"
android:theme="#style/AppMainTheme" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.PlaceActivity"
android:label="#string/title_activity_place"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.ForgotPasswordActivity"
android:label="#string/title_activity_forgot_password"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CVCInfoActivity"
android:label="#string/cvc_text_header"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CategoryActivity"
android:label="#string/title_activity_category"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.MainActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.ProductActivity"
android:label="#string/title_activity_product"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.CategoryActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CheckoutActivity"
android:label="#string/title_activity_checkout"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.PlaceActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.TopupActivity"
android:label="#string/title_activity_topup"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.RegisterCardActivity"
android:label="#string/title_activity_register_card"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.StoreMapActivity"
android:label="#string/store_map_title_activity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.AuthActivity"
android:label="#string/title_activity_auth"
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="nz.co.tish.android.coffeetime.VersionInfoActivity"
android:label="#string/title_activity_version"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.AuthActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.TermsActivity"
android:label="#string/title_activity_terms"
android:screenOrientation="portrait" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="launch.TermsActivity" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.AuthActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.fragment.MapFragment$InnerActivity"
android:screenOrientation="portrait" />
<activity
android:name=".LogoutActivity"
android:label="#string/title_activity_logout"
android:screenOrientation="portrait" />
<activity
android:name=".LikeUsOnFacebookActivity"
android:label="Like Us On Facebook Activity"
android:screenOrientation="portrait" />
</application>
The code of fragment with map(MapFragment):
public class MapFragment extends Fragment implements LocationListener {
private static GoogleMap mapView;
private static final String MAP_ACTIVITY_ID = "map-activity";
private static final String ACTIVITY_MANAGER_BUNDLE_KEY = "activity-manager-bundle-key";
private LocalActivityManager localActivityManager;
private LocationManager locationManager = null;
private Window window;
private ProgressDialog dialog = null;
private String bestProvider = null;
private Criteria criteria = null;
private String keyword = "";
private Place[] places;
private HashMap<String, Place> currentPlaces;
private AsyncTask<Object, Object, CallResult<List<Place>>> location_task;
public static class InnerActivity extends
FragmentActivity {
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.fragment_map);
mapView = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapv2)).getMap();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = window.getDecorView();
keyword = ((MainActivity) getActivity()).getSearchString();
locationManager = (LocationManager) view.getContext().getSystemService(
Activity.LOCATION_SERVICE);
if (locationManager == null) {
DialogUtil.showAlert(view.getContext(),
"Sorry, you do not seem to have GPS!");
return null;
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
((ViewGroup) window.getDecorView().getParent()).removeAllViews();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(ACTIVITY_MANAGER_BUNDLE_KEY,
localActivityManager.saveInstanceState());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = null;
if (savedInstanceState != null) {
bundle = savedInstanceState.getBundle(ACTIVITY_MANAGER_BUNDLE_KEY);
}
localActivityManager = new LocalActivityManager(getActivity(), true);
localActivityManager.dispatchCreate(bundle);
window = localActivityManager.startActivity(MAP_ACTIVITY_ID,
new Intent(getActivity(), InnerActivity.class));
}
#Override
public void onResume() {
super.onResume();
localActivityManager.dispatchResume();
places = ((MainActivity) getActivity()).getPlaces();
location_task = null;
if (places == null) {
AsyncTask<Object, Object, Location> task = new AsyncTask<Object, Object, Location>() {
#Override
protected Location doInBackground(Object... params) {
return locationManager.getLastKnownLocation(bestProvider);
}
#Override
protected void onPostExecute(Location result) {
onLocationChanged(result);
}
};
task.execute();
locationManager.requestLocationUpdates(bestProvider, 1000 * 60 * 5,
25, this);
} else {
setUpMapview(places);
}
}
private void setUpMapview(Place[] places) {
// List<Overlay> mapOverlays = mapView.getOverlays();
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
currentPlaces = new HashMap<String, Place>();
mapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker coffe) {
if (currentPlaces != null) {
Intent intent = new Intent(getActivity(),
PlaceActivity.class);
intent.putExtra("place",
currentPlaces.get(coffe.getTitle()));
startActivity(intent);
}
}
});
for (Place place : places) {
GeoPoint point = new GeoPoint((int) (place.getLatitude()),
(int) (place.getLongitude()));
items.add(point);
Marker placeMarker = mapView
.addMarker(new MarkerOptions()
.position(
new LatLng(place.getLatitude(), place
.getLongitude()))
.title(place.getName())
.icon(BitmapDescriptorFactory.fromResource(place
.isOpen() ? R.drawable.opened
: R.drawable.closed)));
currentPlaces.put(place.getName(), place);
// itemizedOverlay.addOverlay(overlayitem);
// mapOverlays.add(itemizedOverlay);
}
MapUtils.animateToCenter(mapView, items, false);
}
#Override
public void onPause() {
super.onPause();
localActivityManager.dispatchPause(getActivity().isFinishing());
}
#Override
public void onStop() {
super.onStop();
localActivityManager.dispatchStop();
}
#Override
public void onDestroy() {
super.onDestroy();
localActivityManager.dispatchDestroy(getActivity().isFinishing());
}
#Override
public void onDetach() {
super.onDetach();
localActivityManager = null;
}
private class DialogOverlay extends SimpleItemizedOverlay {
public DialogOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker, mapView);
}
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
/*
* Intent intent = new Intent(mapView.getContext(),
* PlaceActivity.class); intent.putExtra("place",
* ((DialogOverlayItem)item).getCurrentPlace());
* startActivity(intent);
*/
return true;
}
}
private class DialogOverlayItem extends OverlayItem {
private Place currentPlace;
public DialogOverlayItem(GeoPoint point, String title, String snippet,
Place place) {
super(point, title, snippet);
setCurrentPlace(place);
}
private Place getCurrentPlace() {
return currentPlace;
}
private void setCurrentPlace(Place currentPlace) {
this.currentPlace = currentPlace;
}
}
}
fragment_map.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
MainActivity:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private SectionsPagerAdapter mSectionsPagerAdapter;
CustomViewPager mViewPager;
private Place[] places;
private String keyword = "";
private boolean checkedOut;
public Place[] getPlaces() {
return places;
}
public void setPlaces(Place[] places) {
this.places = places;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MyFavoritesFragment();
case 1:
return new NearMeFragment();
case 2:
return new MapFragment();
case 3:
return new HistoryFragment(checkedOut);
case 4:
return new WalletFragment();
}
return null;
}
#Override
public int getCount() {
return 5;
}
public int getPageIcon(int position) {
switch (position) {
case 0:
return R.drawable.icon_fav;
case 1:
return R.drawable.icon_near;
case 2:
return R.drawable.icon_map;
case 3:
return R.drawable.icon_hist;
case 4:
return R.drawable.icon_wallet;
}
return 0;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_activity_my_favorites).toUpperCase();
case 1:
return getString(R.string.title_activity_near_me).toUpperCase();
case 2:
return getString(R.string.title_activity_map).toUpperCase();
case 3:
return getString(R.string.title_activity_history).toUpperCase();
case 4:
return getString(R.string.title_activity_wallet).toUpperCase();
}
return null;
}
}
}
This application also has another Activity with map , and the app is crashing on it too. The logcat:
09-29 08:33:50.559 2604-2604/nz.co.tish.android.coffeetime E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nz.co.tish.android.coffeetime/nz.co.tish.android.coffeetime.StoreMapActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4242000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.maps.internal.q.v(Unknown Source)
at com.google.android.gms.maps.internal.q.u(Unknown Source)
at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$b.ex(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
           
StoreMapActivity:
public class StoreMapActivity extends FragmentActivity {
private Place place = null;
private GoogleMap mapView;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return (true);
}
return (super.onOptionsItemSelected(item));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.store_map_activity);
mapView = (GoogleMap) ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.store_mapv2)).getMap();
Serializable placeSer = getIntent().getSerializableExtra("place");
if (placeSer == null
|| !Place.class.isAssignableFrom(placeSer.getClass())) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
return;
}
place = (Place) placeSer;
setUpMapview();
/*List<Overlay> mapOverlays = mapView.getOverlays();
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
SimpleItemizedOverlay itemizedOverlay = new SimpleItemizedOverlay(
place.isOpen() ? openPlace : closedPlace, mapView);
GeoPoint point = new GeoPoint(
(int) (place.getLatitude() * Const.GEOPOINT_CONST),
(int) (place.getLongitude() * Const.GEOPOINT_CONST));
items.add(point);
OverlayItem overlayitem = new OverlayItem(point, place.getName(), "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
// MapUtils.animateToCenter(mapView, items, true);*/
}
private void setUpMapview() {
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
mapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker coffe) {
if (place != null) {
Intent intent = new Intent(StoreMapActivity.this,
PlaceActivity.class);
intent.putExtra("place", place);
startActivity(intent);
}
}
});
GeoPoint point = new GeoPoint((int) (place.getLatitude()),(int)(place.getLongitude()));
items.add(point);
Marker placeMarker = mapView.addMarker(new MarkerOptions().position(
new LatLng(place.getLatitude(), place.getLongitude()))
.title(place.getName())
.icon(BitmapDescriptorFactory.fromResource(place
.isOpen() ? R.drawable.opened
: R.drawable.closed)));
MapUtils.animateToCenter(mapView, items, false);
}
}
store_map_activity.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/store_mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
When I commented the code initializing the maps on these parts of application, it will start working normally.
Please verify that you have added the exceptions to your proguard configuration file. How and what to add is explained in Setting up Google Play Services
For the first, change your fragment from this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
to this:
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
the main difference in this case is
class="com.google.android.gms.maps.SupportMapFragment"
to this:
android:name="com.google.android.gms.maps.SupportMapFragment"
For the second: add the following to the end of your manifest (but beforde the end-tag application!)
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
In your code you can ask for the service with the following:
// check status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
try {
if (status != ConnectionResult.SUCCESS)
{
GooglePlayServicesUtil.getErrorDialog(status, this, RQS_GooglePlayServices).show();
}
} catch (Exception e) {
Log.e("Error: ", + e);
}

java.lang.ClassCastException: android.app.Application - casting

java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.DataDevice
My code:
public class Project extends Activity{
private boolean connection = false;
public Tag tagFromIntent = null;
private Button textRead;
private NFCForegroundUtil nfcForegroundUtil;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
nfcForegroundUtil = new NFCForegroundUtil(this);
this.textRead= (Button) findViewById(R.id.button2);
initListeners();
}
private void initListeners() {
textRead.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (connection == true)
{
DataDevice dataDevice = (DataDevice) getApplication();
dataDevice.setCurrentTag(tagFromIntent);
IsoDep nfca = IsoDep.get(dataDevice.getCurrentTag());
try
{
byte[] read= new byte[] { 0x00};
byte[] ans = null;
nfca.setTimeout(2000);
nfca.connect();
nfca.setTimeout(2000);
if (nfca.isConnected())
{
nfca.setTimeout(2000);
ans = nfca.transceive(read);
try
{
Thread.sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
nfca.close();
String textRead = HexBin.encode(ans);
}
catch (IOException e)
{
Log.i("A", "IOException is: " + e.getMessage());
e.printStackTrace();
}
if (nfca.isConnected())
{
try
{
nfca.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
});
}
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
action = intent.getAction();
tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
connection = true;
}
public void onPause()
{
super.onPause();
nfcForegroundUtil.disableForeground();
}
public void onResume()
{
super.onResume();
nfcForegroundUtil.enableForeground();
if (!nfcForegroundUtil.getNfc().isEnabled())
{
Toast.makeText(
getApplicationContext(),
"Please activate NFC and press Back to return to the application!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
}
My manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="android.app.Application">
<activity
android:name=".StartActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".Project"
android:label="#string/title_activity_main" />
</application>
My error:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.Project.DataDevice
at com.example.project.Project$1.onClick(Project.java:67)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
DataDevice class:
public class DataDevice extends Application
{
private Tag currentTag;
public void setCurrentTag(Tag currentTag) {
this.currentTag = currentTag;
}
public Tag getCurrentTag() {
return currentTag;
}
//(...)
}
I looked for answer on stackOverflow, nothing helped.
Anyone knows what's going on?
DataDevice and NFCUtilForeground works good (in other applications).
The android:name attribute in application tag in your manifest file should point to your DataDevice class. Like:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="your.package.DataDevice">
.........
..........
</application>

Categories

Resources