In my application I can not reuse method I have declared in ArabicUtility class. My intension is to use Arabicutility class to arrange Arabic text. Therefore, what I want is passe string to the method I declared in Arabicutility class and do the conversion.
I think this is basically some problem of my knoweledge of OOP. so help me to correct this.
Here is the method I add to Arabicutility class
public void addTranslate(int rid, TextView txt1) {
String textv = getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
// Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/DroidNaskhBold.ttf");
// txt1.setTypeface(typeFace);
}
I can not declare this method as static since getResources() is a non static.I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);
but When I run the programe it crashes when I go to above activities.
here is the log cat
12-29 10:02:32.561: E/AndroidRuntime(951): FATAL EXCEPTION: main
12-29 10:02:32.561: E/AndroidRuntime(951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxx/com.xxxx.xxx.ShowMessageActivity}: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.os.Looper.loop(Looper.java:137)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 10:02:32.561: E/AndroidRuntime(951): at dalvik.system.NativeStart.main(Native Method)
12-29 10:02:32.561: E/AndroidRuntime(951): Caused by: java.lang.NullPointerException
12-29 10:02:32.561: E/AndroidRuntime(951): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ArabicUtilities.addTranslate(ArabicUtilities.java:252)
12-29 10:02:32.561: E/AndroidRuntime(951): at com.xxxx.xxx.ShowMessageActivity.onCreate(ShowMessageActivity.java:184)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Activity.performCreate(Activity.java:5008)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-29 10:02:32.561: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-29 10:02:32.561: E/AndroidRuntime(951): ... 11 more
No need to declare addTranslate as static to get Resources in non Activity class you just need to pass Current Activity Context by using non activity class constructor or passing as in method as :
public void addTranslate(int rid, TextView txt1,Context context) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
Now you can call addTranslate from Activity class as:
arbic.addTranslate(R.string.butt18title1, txt1,Your_Current_Activity.this);
arbic.addTranslate(R.string.butt18desc1, txt2,Your_Current_Activity.this);
The LogCat shows that Context in ArabicUtility is invalid. Try using the TextView's Context instead:
public void addTranslate(int rid, TextView txt1) {
String textv = txt1.getContext().getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
I had to extend from Activity since I use android methods.Originally It was not defined so.
this is how I try to use above method in other activity class.
If ArabicUtility is not the active Activity then you shouldn't extend Activity, you should try something like this:
public class ArabicUtility {
private Context context;
public ArabicUtility(Context context) {
this.context = context;
}
...
public void addTranslate(int rid, TextView txt1) {
String textv = context.getResources().getString(rid);
txt1.setText(ArabicUtilities.reshapeSentence(textv));
}
}
In your current Activity use:
arbic = new ArabicUtility(this);
arbic.addTranslate(R.string.butt18title1, txt1);
arbic.addTranslate(R.string.butt18desc1, txt2);
Related
I have a ListView and a custom Adapter, but when I set that adapter to my Listview it shows an error like this.
Logcat:
12-29 16:43:43.740: E/AndroidRuntime(27363): FATAL EXCEPTION: main
12-29 16:43:43.740: E/AndroidRuntime(27363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rh.bookmany/com.rh.bookmany.MFragmentContainer}: java.lang.NullPointerException
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2146)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.os.Looper.loop(Looper.java:137)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.main(ActivityThread.java:4944)
12-29 16:43:43.740: E/AndroidRuntime(27363): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 16:43:43.740: E/AndroidRuntime(27363): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 16:43:43.740: E/AndroidRuntime(27363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-29 16:43:43.740: E/AndroidRuntime(27363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-29 16:43:43.740: E/AndroidRuntime(27363): at dalvik.system.NativeStart.main(Native Method)
12-29 16:43:43.740: E/AndroidRuntime(27363): Caused by: java.lang.NullPointerException
12-29 16:43:43.740: E/AndroidRuntime(27363): at com.rh.bookmany.navigationadapter.TheatreListAdapter.getCount(TheatreListAdapter.java:40)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.widget.ListView.setAdapter(ListView.java:466)
12-29 16:43:43.740: E/AndroidRuntime(27363): at com.rh.bookmany.ShowTimeFragment.onCreateView(ShowTimeFragment.java:43)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.BackStackRecord.run(BackStackRecord.java:635)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.Activity.performStart(Activity.java:5197)
12-29 16:43:43.740: E/AndroidRuntime(27363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2094)
12-29 16:43:43.740: E/AndroidRuntime(27363): ... 11 more
Here is my Fragment:
public class ShowTimeFragment extends Fragment {
List<Theatre> theatreList;
ListView lvTheatre;
TheatreListAdapter tlAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating View
View v = inflater.inflate(R.layout.theatre_showtime, container, false);
lvTheatre = (ListView) v.findViewById(R.id.lvTheatre);
tlAdapter = new TheatreListAdapter(getActivity().getBaseContext(),theatreList);
lvTheatre.setAdapter(tlAdapter);
return v;
}
}
and here is my Adapter:
public class TheatreListAdapter extends BaseAdapter {
Context c;
List<Theatre> theatreList;
LayoutInflater lInflater;
public TheatreListAdapter(Context c,List<Theatre> theatreList)
{
Log.d("X","Constructor called");
try {
this.c = c;
this.theatreList = theatreList;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("X",e.getMessage());
}
Log.d("X","Constuctor Passed");
}
#Override
public int getCount() {
return theatreList.size();
}
#Override
public Object getItem(int position) {
return theatreList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("X","getView Called");
try {
//Checking inflater already inflated
if(lInflater==null)
{
lInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//Checking convertView already instantiated
if(convertView==null)
{
convertView = lInflater.inflate(R.layout.theatre_fragment_row, null);
}
//Inst. UI elements
TextView tvTheatre = (TextView) convertView.findViewById(R.id.tvTheatre);
TextView tvShowTimes = (TextView) convertView.findViewById(R.id.tvShowTimes);
//Inst. Theatre for row
Theatre mTheatre = theatreList.get(position);
//Generating showTimes
String showTimes = "";
for(String showTime:mTheatre.getShowTimes())
{
showTimes+=showTime;
}
//Setting collected information to the UI elements
tvTheatre.setText(mTheatre.getTheatre());
tvShowTimes.setText(showTimes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("X",e.getMessage());
}
return convertView;
}
}
When ever I remove the setAdapter method, it works fine, which means no error showing.
But when I'm setting that adapter it shows errors in my logcat.
You get NPE because your theatreList is null. You don't initialize it anywhere.
See this line in your logcat
Caused by: java.lang.NullPointerException
you have not initialized theatreList
tlAdapter = new TheatreListAdapter(getActivity().getBaseContext(),theatreList);
Here is the code mentioned i am trying to insert values in ArrayList and saving arrayList in Session Storage while key is different everytime.Now my target is to get selected value by passing "position" onclick of Spinner.Here are two mthods that are used to save values in SharePreferences i am getting error while trying to retrieve value from getSpinnerSelectedValue method.
public static void setValuesInSession(Context c,String key,List<String> myArrayList)
{
SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor sEdit=sPrefs.edit();
Set<String> set = new HashSet<String>();
Log.i("myArrayList SharePrefereces",""+myArrayList +"::key::"+key);
set.addAll(myArrayList);
sEdit.putStringSet(key,set);
sEdit.commit();
}
public static List<String> getValuesOfSession(Context c,String key)
{
SharedPreferences sPrefs=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor sEdit=sPrefs.edit();
Set<String> set = new HashSet<String>();
set = sPrefs.getStringSet(key,null);
List<String> setList=new ArrayList<String>(set);
return setList;
}
public static void setSpinnerSelectedValue(Context c,String key,String value)
{SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor=pref.edit();
editor.putString(key,value);
}
public static String getSpinnerSelectedValue(Context c,String key)
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(c);
String val = sharedPrefs.getString(key,null);
return val;
}
how i am using this code in Activity:
SessionManager.setValuesInSession(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel().toString(),[0100,0200,0300,0400,0500]);
siteSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
Log.i("Spinner Selected Value", "" + position);
List<String> arrayList = new ArrayList<String>();
arrayList = SessionManager.getValuesOfSession(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel());
Log.i("arrayList Values",""+arrayList);
Log.i("arrayList ",""+arrayList.get(position).toString());
SessionManager.setSpinnerSelectedValue(getActivity().getApplicationContext(),menuFieldInstance.getFieldLabel(),arrayList.get(position).toString());
}
getting Error when trying to retrieve values from "getSpinnerSelectedValue" mthod ?
here is the error List:
02-11 02:23:05.455: E/AndroidRuntime(23432): FATAL EXCEPTION: main
02-11 02:23:05.455: E/AndroidRuntime(23432): java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.mrfs.android.surveyapp.util.SessionManager.getSpinnerSelectedValue(SessionManager.java:85)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.mrfs.android.surveyapp.activities.fragments.SiteFragmentActivity$3.onClick(SiteFragmentActivity.java:513)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.view.View.performClick(View.java:4211)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.view.View$PerformClick.run(View.java:17267)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Handler.handleCallback(Handler.java:615)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.os.Looper.loop(Looper.java:137)
02-11 02:23:05.455: E/AndroidRuntime(23432): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-11 02:23:05.455: E/AndroidRuntime(23432): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 02:23:05.455: E/AndroidRuntime(23432): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-11 02:23:05.455: E/AndroidRuntime(23432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-11 02:23:05.455: E/AndroidRuntime(23432): at dalvik.system.NativeStart.main(Native Method)
onClick of Button i am calling method :
siteSaveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("Session Values",SessionManager.getSpinnerSelectedValue(getActivity().getApplicationContext(),"Police Authority"));
}
Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.
i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.
I am currently developing a soundboard with the ability to add a sound as a favorite. I have a favorites tab and am working on adding sounds to a ListView when clicked. I did some research and came across a post suggesting to use a Set when avoiding duplicate items. In my application class I have a global variable: public Set<String> favorite_list = new TreeSet<String>();. In my ListViewActivity I have
ArrayList favorite_list = (ArrayList) appState.favorite_list;
setListAdapter(new ArrayAdapter<String>(this, R.layout.favorites, favorite_list));
Whenever I click the Favorites Tab, the app force closes. Any help is appreciated.
Thanks, Justin
Edit: LogCat
03-05 21:25:12.996: E/AndroidRuntime(5485): FATAL EXCEPTION: main
03-05 21:25:12.996: E/AndroidRuntime(5485): java.lang.RuntimeException: Unable to start activity ComponentInfo{vartanian.android.epicmealtimepro/vartanian.android.epicmealtimepro.Favorites}: java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.util.ArrayList
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost.setCurrentTab(TabHost.java:346)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.view.View.performClick(View.java:3511)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.view.View$PerformClick.run(View.java:14105)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Handler.handleCallback(Handler.java:605)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.os.Looper.loop(Looper.java:137)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-05 21:25:12.996: E/AndroidRuntime(5485): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 21:25:12.996: E/AndroidRuntime(5485): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 21:25:12.996: E/AndroidRuntime(5485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 21:25:12.996: E/AndroidRuntime(5485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 21:25:12.996: E/AndroidRuntime(5485): at dalvik.system.NativeStart.main(Native Method)
03-05 21:25:12.996: E/AndroidRuntime(5485): Caused by: java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.util.ArrayList
03-05 21:25:12.996: E/AndroidRuntime(5485): at vartanian.android.epicmealtimepro.Favorites.onCreate(Favorites.java:27)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.Activity.performCreate(Activity.java:4465)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-05 21:25:12.996: E/AndroidRuntime(5485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-05 21:25:12.996: E/AndroidRuntime(5485): ... 18 more
Application Class:
public class Data extends Application {
public Set<String> favorite_list = new TreeSet<String>();
}
ListView Class:
public class Favorites extends ListActivity {
/** Called when the activity is first created. */
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Data appState = ((Data)this.getApplication());
if(appState.favorite_list.isEmpty() == true){
appState.favorite_list.add("None");
}
ArrayList favorite_list = (ArrayList) appState.favorite_list;
setListAdapter(new ArrayAdapter<String>(this, R.layout.favorites, favorite_list));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}