Android Activity not Responding - android

I have 3 activities in my app and when user press next button, next activity is shown to user, Now everything works fine except when it reaches the last activity i.e. when user presses next on 2nd last activity, an error message is shown that app has stopped working and there is no error in LogCat, following is the .java file of my Final Activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class FinalActivity extends ActionBarActivity {
TextView name,address,phone,email,dob,matg,mati,interg,interi,graddeg,gradi,cgpa,skills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
name=(TextView)findViewById(R.id.fname);
address=(TextView)findViewById(R.id.faddress);
phone=(TextView)findViewById(R.id.fphone);
email=(TextView)findViewById(R.id.femail);
dob=(TextView)findViewById(R.id.fdob);
matg=(TextView)findViewById(R.id.matricgrade);
mati=(TextView)findViewById(R.id.matricinst);
interg=(TextView)findViewById(R.id.intergrade);
interi=(TextView)findViewById(R.id.interinst);
graddeg=(TextView)findViewById(R.id.graddegree);
cgpa=(TextView)findViewById(R.id.gradcgpa);
skills=(TextView)findViewById(R.id.iskills);
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills=in.getStringExtra("skills");
name.setText("Name : "+bo.name);
address.setText("Address : "+bo.address);
email.setText("Email : "+bo.email);
phone.setText("Phone : "+bo.phone);
dob.setText("DOB : "+bo.dob);
matg.setText("Matric Grade : "+bo.mg);
mati.setText("Institution : "+bo.mi);
interg.setText("Inter Grade : "+bo.ig);
interi.setText("Institution : "+bo.ii);
graddeg.setText("Graduation Degree : "+bo.gg);
gradi.setText("Institution : "+bo.gi);
skills.setText("Skills : "+bo.skills);
}
}
Here is the xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.first.FinalActivity"
tools:ignore="MergeRootFrame" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/faddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/femail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/fdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricgrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/matricinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/intergrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/interinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/graddegree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradinst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/gradcgpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/iskills"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="#+id/hobbies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
</FrameLayout>
Here i'm calling the final activity
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity2 extends ActionBarActivity {
Button next3;
EditText s1,s2,s3,s4,s5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
next3=(Button)findViewById(R.id.next3);
s1=(EditText)findViewById(R.id.sk1);
s2=(EditText)findViewById(R.id.sk2);
s3=(EditText)findViewById(R.id.sk3);
s4=(EditText)findViewById(R.id.sk4);
s5=(EditText)findViewById(R.id.sk5);
next3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=in.getStringExtra("mgrade");
bo.mi=in.getStringExtra("minst");
bo.ig=in.getStringExtra("igrade");
bo.ii=in.getStringExtra("iinst");
bo.gg=in.getStringExtra("gdeg");
bo.gi=in.getStringExtra("ginst");
bo.cgpa=in.getStringExtra("cgpa");
bo.skills+=s1.getText().toString()+","+s1.getText().toString()+","+s2.getText().toString()+","+s3.getText().toString()+","+s4.getText().toString()+","+s5.getText().toString();
Intent i=new Intent(getApplicationContext(),FinalActivity.class);//this,same
System.out.println(bo.name);
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
i.putExtra("skills", bo.skills);
startActivity(i);
}
});
}
Here is Activity1
package com.example.first;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class Activity1 extends ActionBarActivity {
Button next2;
EditText matGrade,matInst,iGrade,iInst,gDegree,gInst,gCgpa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
next2=(Button)findViewById(R.id.next2);
matGrade=(EditText)findViewById(R.id.mgrade);
matInst=(EditText)findViewById(R.id.minst);
iGrade=(EditText)findViewById(R.id.igrade);
iInst=(EditText)findViewById(R.id.iinst);
gDegree=(EditText)findViewById(R.id.gdegree);
gInst=(EditText)findViewById(R.id.ginst);
gCgpa=(EditText)findViewById(R.id.cgpa);
next2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=getIntent();
UserBO bo=new UserBO();
bo.name=in.getStringExtra("name");
bo.address=in.getStringExtra("address");
bo.email=in.getStringExtra("email");
bo.phone=in.getStringExtra("phone");
bo.dob=in.getStringExtra("dob");
bo.mg=matGrade.getText().toString();
bo.mi=matGrade.getText().toString();
bo.ig=iGrade.getText().toString();
bo.ii=iGrade.getText().toString();
bo.gg=gDegree.getText().toString();
bo.gi=gDegree.getText().toString();
bo.cgpa=gDegree.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity2.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
i.putExtra("mgrade", bo.mg);
i.putExtra("minst", bo.mi);
i.putExtra("igrade", bo.ig);
i.putExtra("iinst", bo.ii);
i.putExtra("gdeg", bo.gg);
i.putExtra("ginst", bo.gi);
i.putExtra("cgpa", bo.cgpa);
startActivity(i);
}
});
}
}
Here is the main Activity
package com.example.first;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button next1;
EditText name,address,email,phone,date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next1=(Button)findViewById(R.id.next1);
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.address);
email=(EditText)findViewById(R.id.email);
phone=(EditText)findViewById(R.id.phone);
date=(EditText)findViewById(R.id.date);
next1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UserBO bo=new UserBO();
bo.name=name.getText().toString();
bo.address=address.getText().toString();
bo.email=email.getText().toString();
bo.phone=phone.getText().toString();
bo.dob=date.getText().toString();
Intent i=new Intent(getApplicationContext(),Activity1.class);//this,same
i.putExtra("name", bo.name);
i.putExtra("address", bo.address);
i.putExtra("email", bo.email);
i.putExtra("phone", bo.phone);
i.putExtra("dob", bo.dob);
startActivity(i);
}
});
}
}
Please help me find the mistake

I think you have null pointer exception. Maybe one of your controls is missed in your xml layout. For example you are finding "next1" in your code and using its setOnClickListener method while it is not defined in you xml layout so its object is null.
I recommend you put the content of onCreate method in a try-catch and debug your application.

Related

onListItemClick not being invoked

I am following a online course and I can across this exercise that for some reason is not working. The onListItemClick() is not being invoked. I have tried troubleshooting this but I couldn't get rid of the issue
Please help me solve this issue.
My code for MainActivity.java is
package hk.ust.cse.comp107x.greetfriend;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity{
String names[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
names=getResources().getStringArray(R.array.friends);
//setListAdapter((ListAdapter)new ArrayAdapter<String>(this,R.layout.friend_item,names));
setListAdapter( new ArrayAdapter<String>(this, R.layout.friend_item, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent in=new Intent(this,ShowMessage.class);
in.putExtra("message","Good Day "+names[(int) id]+"!!");
startActivity(in);
}
}
Code for ShowMessage.java is
package hk.ust.cse.comp107x.greetfriend;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowMessage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_message);
Intent in=getIntent();
String message=in.getStringExtra("message");
TextView textMessage=(TextView)findViewById(R.id.textMessage);
textMessage.setText(message);
}
}
My code for friend.xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:autoText="true"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textview">
</TextView>
And finally my code for strings.xml is
<resources>
<string name="app_name">GreetFriend</string>
<string-array name="friends">
<item>John</item>
<item>Paul</item>
<item>George</item>
<item>Ringo</item>
</string-array>
</resources>
Use the following code for your Friend.xml and run the code...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="Friend Name"
android:gravity="center_vertical|center_horizontal"
android:textSize="24sp"
android:padding="20dp"
android:id="#+id/textviewfriend">
</TextView>
</LinearLayout>

Fatal Exception : Main on android app

I have a problem with my project. If you can to solve this problem please help me..
This is main code
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.util.FloatMath;
import android.util.Log;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.Math;
import com.kalkulator.R;
public class MainActivity extends Activity implements OnClickListener {
private String str1="",str2="",str3="",op="";
private Float num1,num2,num3,num4,num5;
private long a,b=1;
EditText ed;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ed=(EditText)findViewById(R.id.EditText01);
View[] keys=new View[30];
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
str3=ed.getText().toString();
if(str3.contains("E"))
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
a=Long.parseLong(str3);
if(a>25)
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
for(long i=1;i<=a;i++)
{
b*=i;
}
ed.setText(String.valueOf(b));
b=1;
break;
......
And this is the xml code
<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: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" >
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:cursorVisible="false"
android:gravity="right"
android:textStyle="italic" >
</EditText>
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/EditText01"
android:layout_below="#+id/EditText01"
android:layout_marginTop="28dp"
android:stretchColumns="*" >
<TableRow
android:id="#+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="n!" />
.......
I don't know why, but when i run this project on AVD_for_3_7_FWVGA_slide, i have notification on log cat FATAL EXCEPTION : Main.
Thanks
//setContentView(R.layout.main); must not be commented, and you should consider moving the orientation to your AndroidManifest file
Also,
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
You're calling findViewById on the keys values with index > which gets you a NullPointerException.

android layout is not displaying

I know its probably something simple I am missing here, but for some reason I can't seem to get my layout to display when I run my app using the emulator. Any feedback would be great. Thanks
my fragment
package com.pctoolman.planme.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class PlanMeMainFragment extends Fragment {
private Button mNewButton, mExistingButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.planme_main_fragment, container, false);
mNewButton = (Button)v.findViewById(R.id.new_event_button);
mNewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
getActivity().startActivity(myIntent);
}
});
return v;
}
}
my layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ff3a0b">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/plan_me"
android:textSize="36dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center|top"
android:textAlignment="center"
android:textColor="#2c58ff"
android:id="#+id/textView">
</TextView>
</LinearLayout>
<Button
android:id="#+id/new_event_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:padding="5dp"
android:text="#string/new_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
<Button
android:id="#+id/existing_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="#string/existing_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
</RelativeLayout>
here is the activity file
package com.pctoolman.planme.app;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class PlanMeMainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.planme_main_activity);
}
}
You should make a transatcion via FragmentManager to show your fragment in the activity. Suppose you have container (FrameLayout, for example) with id = fragment_container in your planme_main_activity.xml. If so, you should add the following after setContentView(R.layout.planme_main_activity);:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new PlanMeMainFragment())
.commit();
But if you declared fragment directly in XML, please post your planme_main_activity.xml
Maybe it's because you haven't positioned the buttons properly. It's easy to move things around in a relative layout. You don't need to use gravity. You can use:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btn"
android:layout_below="#+id/btn"
I'm assuming you cut out the closing tag for the RL.

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