Java null pointer exception while using google maps in Android - android

package com.crumbin.tabs;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.client.HttpClient;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.webkit.WebView;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class ExploreActivity extends MapActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(com.app.main.R.layout.user_main_tab_explore);
MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView);
mv.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="Mykey"/>
</LinearLayout>
This code gives me a java null pointer exception. I debugged it and found out that Mapview mv is null.
Shouldn't this code just display the map and no overlay/data on it? Or am I missing something here?

This line
MapView mv = (MapView)findViewById(com.app.main.R.id.myMapView);
needs to be
MapView mv = (MapView)findViewById(R.id.myMapView);
To explain more, you are requesting an ID of a View that is on the R class in the package com.app.main, when you need to request it from the R class in your own package, which can be omitted.

Related

Cannot resolve symbol image_view in Wear OS project

I am getting a "cannot resolve symbol 'image_view'" and cannot figure out why.
Here is my main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.BoxInsetLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_grey"
android:padding="#dimen/box_inset_layout_padding"
tools:context=".MainActivity"
tools:deviceIds="wear">
<android.support.v7.widget.FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/inner_frame_layout_padding"
app:boxedEdges="all" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/image_view" />
</android.support.v7.widget.FrameLayout>
</androidx.wear.widget.BoxInsetLayout>
and my the beginning part of my MainActivity.java:
package com.example.weartest3;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.CapabilityClient;
import com.google.android.gms.wearable.CapabilityInfo;
import com.google.android.gms.wearable.DataClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItem;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageClient;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
public class MainActivity extends WearableActivity implements DataClient.OnDataChangedListener,
MessageClient.OnMessageReceivedListener,
CapabilityClient.OnCapabilityChangedListener {
private TextView mTextView;
private ImageView mImageView;
private static final String IMAGE_KEY = "image";
private static final String TEXT_KEY = "text";
private static final String TAG="MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
mImageView = (ImageView) findViewById(R.id.image_view);
// Enables Always-on
setAmbientEnabled();
}
The line:
mImageView = (ImageView) findViewById(R.id.image_view); is showing image_view in red and that's where the error is.
I notice it does not autofind .text in:
mTextView = (TextView) findViewById(R.id.text);
but it is not showing in red.
I also had problems with:
app:boxedEdges="all"
until I added:
android.support.v7.widget
in:
Which seems strange to me, but that will be a different post.

Erro on Display Navigation Drawer in other activities?

I use this tutorial to make Navigation Drawer: http://www.tutecentral.com/android-custom-navigation-drawer/
so now i want to add my navigation to other activities but unfortunately i cant.
i also follow this : How to Display Navigation Drawer in all activities?
and do this on my second activity but it have some error:
package com.example.uniapp;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ShowAll extends Description {
MyDatabase MyDataBase;
SQLiteDatabase mydb;
TextView title,content;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.showall, null, false);
mDrawerLayout.addView(contentView, 0); //null pointer error
}
}
Do you have any suggestion?
This means your drawerLayout is null. This is probably because it can;t find it. If you want your navigationdrawer in multiple activities, you also need to add the drawerLayout the all the activities its xml files.

Android multi stroke gesture not working

I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:
1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder.
2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.
Code:
xml file:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:fadeOffset="5000"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple" />
</LinearLayout>
java file:
package com.example.gesture_letter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
gestureoverlayview.setGestureVisible(true);
gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));
gestureoverlayview.addView(inflate);
gestureoverlayview.addOnGesturePerformedListener(this);
mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
if(!mLibrary.load())
{
Log.i("debug","FAILED");
}
else
{
Log.i("debug","LOADED");
}
Log.i("debug","OnCreate");
setContentView(gestureoverlayview);
}
#Override
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
if(predictions.size()>0)
{
Prediction prediction=predictions.get(0);
if(prediction.score>1.0)
{
String s=prediction.name;
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
else
if(prediction.score<1.0)
{
Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
}
}
}
}
Thank you.
I also would like a good resource for gesture application building.thank you
I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :
gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)
(in main_activity.java file for newbies)
Then you'll see it works.
I think its a bug, I'm not sure.I spent weeks hovering on this :(
The problem is that you create a new GestureOverlayView :
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
You should call the view defined in the xml with findViewById(R.id.gesture_overlay);

Android - Spinner, onItemSelected(...) not being called

Here's the code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.project.locationapp.model.Device;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class SelectDevice extends Activity implements OnItemSelectedListener {
private Spinner deviceSpinner;
private List<Device> deviceList;
private ArrayAdapter<Device> deviceAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_device);
deviceList = new ArrayList<Device>();
try {
deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_dropdown_item, deviceList);
deviceSpinner.setAdapter(deviceAdapter);
deviceSpinner.setOnItemSelectedListener(this);
DataAsyncTask loadDevices = new DataAsyncTask();
loadDevices.execute(new String[] { WebServiceURL.WEB_SERVICE + WebServiceURL.DEVICES + WebServiceURL.ALL });
} catch (Exception e){
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
#Override
public void onItemSelected(AdapterView<?> a, View v, int position,
long id) {
Log.d(TAG, "called!");
Intent intent = new Intent(this, ViewTrips.class);
intent.putExtra("device_id", deviceAdapter.getItem(position).getId());
startActivity(intent);
}
}
DeviceAdapter class:
import java.util.List;
import com.project.locationapp.model.Device;
import android.content.Context;
import android.widget.ArrayAdapter;
public class DeviceAdapter extends ArrayAdapter<Device> {
public DeviceAdapter(Context context, int textViewResourceId,
List<Device> objects) {
super(context, textViewResourceId, objects);
}
}
Activity layout xml:
<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=".SelectDevice" >
<TextView
android:id="#+id/instructions_device_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/select_device_instructions"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp" />
<Button
android:id="#+id/start_service_button"
android:layout_below="#id/instructions_device_select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="thisDevice"
android:text="#string/this_device"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp" />
<Spinner
android:id="#+id/select_device_spinner"
android:layout_below="#id/start_service_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_device"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:drawSelectorOnTop = "true" />
</RelativeLayout>
I am waiting to get this working properly, then I am going to customise DeviceAdapter further.
The Activity implements OnItemSelectedListener, hence the override of onItemSelected(...), which isn't being called at all. No errors in LogCat. Spinner is defined in the Activity's layout xml and displays and populates fine. Any advice to fix this would be great.
Thank you.
It might be the spinner layouts, I can see you didn't set a dropdown view for the adapter.
Could you try to initialize your spinner like this:
deviceAdapter = new DeviceAdapter(this, android.R.layout.simple_spinner_item, deviceList);
deviceSpinner.setAdapter(deviceAdapter);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceSpinner.setOnItemSelectedListener(this);
I was facing the same problem today. Then I realized I am testing on emulator. When I tested the same app it worked. I am posting this if someone is trying to get call back for spinner over emulator it did not work for me. you may also check the same. It works on real device

android call log

With the following codes, the call log displayed is empty.Why is that soo??
package fypj.c;
import java.security.Provider;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class c extends ListActivity {
private SimpleCursorAdapter myAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] column = new String[] {android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.DURATION, android.provider.CallLog.Calls.TYPE};
int[] names = new int[] {R.id.CLName, R.id.CLDuration, R.id.CLType};
myAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, cursor, column, names);
setListAdapter(myAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLName"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLType"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CLDuration"
/>
</LinearLayout>
According to SimpleCursorAdapter,
"This constructor is deprecated.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader. "
Try an alternative to rule out any problem with your code.

Categories

Resources