The title is the error i've been receiving when I try (I think) to add an object to an arraylist
Here is the start of my code up to the onCreate method. I'm trying to use a button to add an object of "TimeTableEntries" to the "timetableItemsList" arraylist
*import statements*
public class Main extends AppCompatActivity {
TimetableEntriesClass timetableEntry;
private static final int Add_New_Item = Menu.FIRST;
private static final int Remove_Item = Menu.FIRST + 1;
private boolean addingNew = false;
private ListView timetableListView;
private EditText moduleCodeText;
private EditText DayOfWeekText;
private EditText StartTimeText;
private EditText DurationText;
private EditText SessionTypeText;
private EditText RoomText;
private Button Add;
private ArrayList<TimetableEntriesClass> timetableItemsList;
private TimetableEntriesAdapter aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
timetableListView = (ListView)findViewById(R.id.myListView);
moduleCodeText = (EditText)findViewById(R.id.ModuleCode);
DayOfWeekText = (EditText)findViewById(R.id.DayOfWeek);
StartTimeText = (EditText)findViewById(R.id.StartTime);
DurationText = (EditText)findViewById(R.id.Duration);
SessionTypeText = (EditText)findViewById(R.id.SessionType);
RoomText = (EditText)findViewById(R.id.Room);
Add = (Button)findViewById(R.id.AddButton);
timetableItemsList = new ArrayList<TimetableEntriesClass>();
int resID = R.layout.timetable_view;
aa = new TimetableEntriesAdapter(this, resID, timetableItemsList);
timetableListView.setAdapter(aa);
Add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimetableEntriesClass item = new TimetableEntriesClass(moduleCodeText.getText().toString(), DayOfWeekText.getText().toString(),
Integer.parseInt(StartTimeText.getText().toString() + ""), Integer.parseInt(DurationText.getText().toString() + ""),
SessionTypeText.getText().toString(), RoomText.getText().toString());
timetableItemsList.add(item);
aa.notifyDataSetChanged();
moduleCodeText.setText("#string/module_code");
DayOfWeekText.setText("#string/day_of_week");
StartTimeText.setText("#string/start_time");
DurationText.setText("#string/duration");
SessionTypeText.setText("#string/type_of_session");
RoomText.setText("#string/room");
}
});
registerForContextMenu(timetableListView);
}
04-04 15:19:11.499 2082-2119/com.example.daniel.mobileassignment E/Surface: getSlotFromBufferLocked: unknown buffer: 0xad2fefd0
04-04 15:19:13.523 2082-2082/com.example.daniel.mobileassignment W/ResourceType: No package identifier when getting value for resource number 0x00000001
04-04 15:19:13.532 2082-2082/com.example.daniel.mobileassignment D/AndroidRuntime: Shutting down VM
04-04 15:19:13.547 2082-2082/com.example.daniel.mobileassignment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daniel.mobileassignment, PID: 2082
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:312)
I found the problem, it was the setText in my ArrayAdapter, it was looking for a String resource ID when it just needed an int!
Related
i'm trying to get the radiobutton text from the radiogroup but issue is: it didn't get the value. i searched on other links but didn't helpful for me.
private EditText FirstName;
private EditText LastName;
private RadioGroup Gender;
private EditText MobileNo;
private RadioButton btn_gender;
private Button BtnNext;
private ProgressBar progressBar;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender=(RadioGroup) findViewById(R.id.radio_group);
MobileNo=(EditText)findViewById(R.id.btn_mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
int selectedId=Gender.getCheckedRadioButtonId();
btn_gender=(RadioButton)findViewById(selectedId);
BtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname=LastName.getText().toString().trim();
String gender=btn_gender.getText().toString().trim();
String phoneno=BtnNext.getText().toString().trim();
registration_form reg=new registration_form();
reg.setFirstName(fname);
reg.setLastName(lname);
reg.setGender(gender);
reg.setmobileNo(phoneno);
ref.child("registration_form").setValue(reg);
Toast.makeText(getApplicationContext(),"Registration Error", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
});
}
}
and i'm getting this error:
FATAL EXCEPTION: main
Process: associate.aclass.associate1, PID: 28847
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.CharSequence android.widget.RadioButton.getText()' on a
null object reference
at
associate.aclass.associate1.registration_form_handler$2.onClick(registration_form_handler.java:81)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
btn_gender is null you need to do this outside of Gender.setOnCheckedChangeListener to initialize btn_gender
btn_gender = (RadioButton)findViewById(R.id.radioButtonID); // use your id
I've MainActivity as following:
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
TextView textView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb =new DatabaseHelper(this);
Cursor res= myDb.getAllData();
if(res.getCount()==0)
{
textView = (TextView)findViewById(R.id.textView);
textView.setText("No Reminders");
button = (Button)findViewById(R.id.button);
button.setText("Create a Reminder");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { //on click 2nd activity is called.
Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
//myIntent.putExtra("key", value); //Optional parameters
startActivity(myIntent);
}
});
return;
}
StringBuffer buffer=new StringBuffer();
while(res.moveToNext())
{
buffer.append("Id : " +res.getString(0)+"\n");
buffer.append("Event : " +res.getString(1)+"\n");
buffer.append("Location : " +res.getString(2)+"\n");
}
textView = (TextView)findViewById(R.id.textView);
textView.setText(buffer.toString());
}
}
I've another activity named main2activity. But when I press the button in 1st activity, The app crashes and log gives NullPointerException. But this is not duplicate of any other such NullPointerException questions.
Because when I run only my second activity Main2Activity, separately, it does not give to this error. But when It is called from mainActivity, It is giving error. I can't understand why setting listener on button giving NullPointerException.
My Main2Activity is :
public class Main2Activity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private GoogleApiClient mGoogleApiClient;
public TextView tv4;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
private boolean flag = false;
DatabaseHelper myDb;
EditText newevent;
Button submit;
Button viewremainders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_main);
myDb =new DatabaseHelper(this);
newevent=(EditText)findViewById(R.id.newEvent);
submit=(Button)findViewById(R.id.submit);
viewremainders=(Button)findViewById(R.id.view);
Button pickerButton = (Button) findViewById(R.id.pickerButton);
tv4 = (TextView)findViewById(R.id.textView4);
pickerButton.setOnClickListener(new View.OnClickListener() { // <-- Error is here. (NullPointerException.)
#Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
Intent intent = intentBuilder.build(Main2Activity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
AddData();
viewremainders();
}
Here I've mentioned where the error is occurring in comment. And I've other methods with this but only included necessary part.
Thanks in advance!
EDIT:
Log:
08-18 21:48:05.421 29655-29655/com.example.kaushal28.locationbasedreminder E/test: Exception
08-18 21:48:05.424 29655-29655/com.example.kaushal28.locationbasedreminder E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kaushal28.locationbasedreminder/com.example.kaushal28.locationbasedreminder.Main2Activity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.kaushal28.locationbasedreminder.Main2Activity.onCreate(Main2Activity.java:85)
at android.app.Activity.performCreate(Activity.java:5182)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
You are setting the same layout in both activities. Make sure and add correct layout.
setContentView(R.layout.activity_main);
public class Main2Activity extends AppCompatActivity {
.
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_main); // here is issue same layout is used for both activity
}
}
I'm faced with this problem
java.lang.UnsupportedOperationException
What I want to do is easy, I'll show you an example.
MyList
Blue
Red
Green
When I make an OnItemLongClickListener() for example on Green, my list should looks like :
MyList
Green
What I've tried is
final ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), android.R.layout.simple_list_item_1, FileBackup.getFilesFound());
adapter.setNotifyOnChange(true);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView <? > arg0, View arg1, final int arg2, long arg3) {
String itemValue = (String) lv.getItemAtPosition(arg2);
Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());
FileBackup.setNameFile(itemValue);
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.user_file_choose);
Button button = (Button) dialog.findViewById(R.id.btOk);
Button button2 = (Button) dialog.findViewById(R.id.btKo);
TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
//Clear all the ListView
lv.setAdapter(null);
}
});
}
});
It's working fine, if I want to clear the ListView, but in my case I DON'T WANT TO, I tried to add item or remove with :
adapter.addAll("Something");
or
lv.removeViewAt(2);
And then call adapter.notifyDataSetChanged(); but it gives the same error every time I try to.
What I'm doing wrong?
EDIT
Now the code looks like :
final ArrayAdapter < String > adapter = new ArrayAdapter < String > (getActivity(), Arrays.asList(FileBackup.getFilesFound()));
adapter.setNotifyOnChange(true);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView <? > arg0, View arg1, final int arg2, long arg3) {
String itemValue = (String) lv.getItemAtPosition(arg2);
Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());
FileBackup.setNameFile(itemValue);
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.user_file_choose);
Button button = (Button) dialog.findViewById(R.id.btOk);
Button button2 = (Button) dialog.findViewById(R.id.btKo);
TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
//Clear all the ListView
lv.setAdapter(null);
//Trying to add that current item clicked
adapter.addAll(FileBackup.getNameFile());
adapter.notifyDataSetChanged();
}
});
}
});
And this is the LogCat error :
06-10 20:14:43.531 8072-8072/info.androidhive.tabsswipe E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.tabsswipe, PID: 8072
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at java.util.Collections.addAll(Collections.java:2583)
at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:211)
at info.androidhive.tabsswipe.BackupWA$1$1.onClick(BackupWA.java:80)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Where it's pointing this --> adapter.addAll(FileBackup.getNameFile()); but it's not null I also tried to adapter.addAll("RED");
EDIT2
I'm trying to keep the name what I've clicked to FileBackup.getFilesFound() so I can create my adapter again I'm doing it with :
String[] itemvalues = (String[]) lv.getItemAtPosition(arg2);
FileBackup.setFilesFound(itemvalues);
And then I'm doing :
adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
//Add the name
adapter.notifyDataSetChanged();
On my class is declared as String[] and the error is :
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.String[]
EDIT3
It's giving the same error every time I want to update the list... #Y.S. said that It's better to post all of my code to see what's going on, because somewhere I'm using String[] instead of ArrayList<String>
That's my FileBackup.java
public class FileBackup {
private String path;
private String pathFile;
private File FileToBackUp;
private String SDpath;
private String[] FilesFound;
private String nameFile;
public String getPathFile() {
return pathFile;
}
public void setPathFile(String pathFile) {
this.pathFile = pathFile;
}
public String getNameFile() {
return nameFile;
}
public void setNameFile(String nameFile) {
this.nameFile = nameFile;
}
public FileBackup(String path){
this.path = path;
}
public File getFileToBackUp() {
return FileToBackUp;
}
public void setFileToBackUp(File fileToBackUp) {
FileToBackUp = fileToBackUp;
}
public String[] getFilesFound() {
this.FilesFound = FilesFound();
return FilesFound;
}
public String[] FilesFound() {
File dir = new File(this.path);
File[] filelist = dir.listFiles();
String[] theNamesOfFiles = new String[filelist.length];
for (int i = 0; i < theNamesOfFiles.length; i++) {
theNamesOfFiles[i] = filelist[i].getName();
}
return theNamesOfFiles;
}
And this is how I have the List of the files.
final String itemValue = (String) lv.getItemAtPosition(arg2);
Log.d("Item clicked --> ", lv.getItemAtPosition(arg2).toString());
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.user_file_choose);
Button button = (Button) dialog.findViewById(R.id.btOk);
Button button2 = (Button) dialog.findViewById(R.id.btKo);
FileBackup.setNameFile(itemValue);
TextView tvBackUpFile = (TextView) dialog.findViewById(R.id.textViewContentDialog);
tvBackUpFile.setText("Do you want to backup " + FileBackup.getNameFile() + " ?");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//The complete path of the fail
FileBackup.setPathFile(GlobalConstant.path + "/" + FileBackup.getNameFile());
//Converting the path of the file to a File and putting it to FileToBackUp
FileBackup.setFileToBackUp(PathToFile(FileBackup.getPathFile()));
dialog.dismiss();
//Clear all the ListView
lv.setAdapter(null);
//Add the name
BackUpFile.setEnabled(true);
}
});
I tried to do something like :
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//The complete path of the fail
FileBackup.setPathFile(GlobalConstant.path + "/" + FileBackup.getNameFile());
//Converting the path of the file to a File and putting it to FileToBackUp
FileBackup.setFileToBackUp(PathToFile(FileBackup.getPathFile()));
///// THIS IS NEW ///////////
String itemvalues = (String) lv.getItemAtPosition(arg2);
FileBackup.setFilesFound(new String[] {
itemvalues
});
adapter.addAll(FileBackup.getFilesFound());
//Add the name
adapter.notifyDataSetChanged();
//////////TIL HERE/////////////
dialog.dismiss();
//Clear all the ListView
//Add the name
BackUpFile.setEnabled(true);
}
});
And this is the LogCat
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at java.util.AbstractCollection.addAll(AbstractCollection.java:76)
at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195)
at info.androidhive.tabsswipe.BackupWA$1$1.onClick(BackupWA.java:88)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Where is pointing
adapter.addAll(Arrays.asList(FileBackup.getFilesFound()));
Also I've tried with
adapter.addAll(FileBackup.getFilesFound());
But it's giving the same error... Maybe is that I'm doing this on a not correct place...
Look this answer:
ArrayAdapter
So basically as in this answer said: "The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified."
So it easy, create your own ArrayList, in your case of type String, and then assign this to CustomAdapter, then you can just remove items from your ArrayList and notifyDataSetChanged() should works.
Regards
I have 2 activities, and I'm passing edit number editText data to 2nd activity with intent. But in second activity I can't convert data from String to int with any functions. Here is my code:
This is main function:
public class MainActivity extends ActionBarActivity {
public EditText mAge;
public Button mCalculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAge = (EditText) findViewById(R.id.numberImputEditText);
mCalculate = (Button) findViewById(R.id.calculateButton);
mCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String age = mAge.getText().toString();
Intent mIntent = new Intent(getApplicationContext(),FinalActivity.class);
mIntent.putExtra("age", age);
startActivity(mIntent);
}
});
}
}
and this is 2nd activity where is data passed:
public class FinalActivity extends Activity {
public Button mCalculateAgain;
public TextView mMinAge;
public TextView mMaxAge;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
Intent mIntent = getIntent();
String age = mIntent.getStringExtra("age");
mCalculateAgain = (Button) findViewById(R.id.calculateAgainButton);
mMinAge = (TextView) findViewById(R.id.minAgeTextView);
mMaxAge = (TextView) findViewById(R.id.maxAgeTextView);
Integer i = Integer.parseInt(age);
mMinAge.setText((i/2)+7);
mMaxAge.setText((i-7)*2);
Toast.makeText(getApplicationContext(), age, Toast.LENGTH_LONG).show();
mCalculateAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(mIntent);
}
});
}
}
here's the log:
`java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robigroza.halfyourage/com.robigroza.halfyourage.FinalActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x12
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
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:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x12
at android.content.res.Resources.getText(Resources.java:1058)
at android.widget.TextView.setText(TextView.java:3857)
at com.robigroza.halfyourage.FinalActivity.onCreate(FinalActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5203)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
There's a few types of TextView.setText(...). One takes a CharSequence, which is probably what you expected to use, and another commonly used variant takes an int. The int form expects the parameter to be a resource ID.
mMinAge.setText((i/2)+7);
mMaxAge.setText((i-7)*2);
Given that (i/2)+7 isn't likely to resolve to a string resource, you could do:
mMinAge.setText("" + (i/2)+7);
mMaxAge.setText("" + (i-7)*2);
or better:
mMinAge.setText(String.valueOf((i/2)+7));
mMaxAge.setText(String.valueOf((i-7)*2));
Use this code in MainActivity.java
int age = Integer.parseInt(mage.getText().toString());
Intent i = new Intent(MainActivity.this,FinalActivity.class);
i.putExtra("age", age);
startActivity(i);
Code for FinalActivity .java
Bundle extras = getIntent().getExtras();
int age = extras.getInt("age");
In this way you can send integer value to other activity.
EnJoY coding :)
ISSUE:
I'm getting an error in eclipse stating `Cannot make a static reference to the non-static method setImageBitmap(Bitmap) from the type ImageView
SOURCE:
public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
Drawable selstation_up_btn;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
Drawable d = getResources().getDrawable(selstation_up_btn);
imageView.setImageBitmap(((BitmapDrawable) d).getBitmap());
CITATION / EXAMPLE SOURCE / INITIAL ISSUE:
Footer Image Will Not Size Correctly
EDIT (In Response to Ramaral)
public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
Drawable selstation_up_btn;
ImageView imageView;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
ImageView myImageView = (ImageView) findViewById(R.id.selstation_up_btn);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);
ISSUE AFTER EDIT (In Response to Ramaral)
12-02 10:28:23.619: E/AndroidRuntime(24278): FATAL EXCEPTION: main
12-02 10:28:23.619: E/AndroidRuntime(24278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idg2/com.idg.omv.Home}: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.os.Looper.loop(Looper.java:137)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.main(ActivityThread.java:4950)
12-02 10:28:23.619: E/AndroidRuntime(24278): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
12-02 10:28:23.619: E/AndroidRuntime(24278): at dalvik.system.NativeStart.main(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278): Caused by: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.idg.omv.Home.onCreate(Home.java:89)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.Activity.performCreate(Activity.java:5179)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-02 10:28:23.619: E/AndroidRuntime(24278): ... 11 more
This is because setImageBitmap seems not to be a static method but you call it like one. You probably need an instance of ImageView and call setImageBitmap on that instance.
I.e. you need to get the instance of the ImageView from your view and call then call the method.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());
}
You could also use the setImageDrawable method which might be more appropriate in your case.
#Override
protected void onCreate(Bundle savedInstanceState) {
...
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageDrawable(d);
}
Since your error occurs because the drawable is null try to change this
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());
to this
Bitmap d = BitmapFactory.decodeResource(context.getResources(),
R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(d);
Your setImageBitmap() method is not static but you are trying to call it in a static way. That's the problem.
Try
reference.setImageBitmap(((BitmapDrawable) d).getBitmap()); // if you have a reference to it
or
setImageBitmap(((BitmapDrawable) d).getBitmap()); // if it is in your current class
If your ImageView is in R.layout.home.
Try this
ImageView myImageView = (ImageView) findViewById(R.id.your_image_id);
myImageView.setImageBitmap(((BitmapDrawable) d).getBitmap());
Edit
imageView = (ImageView) findViewById(R.id.your_image_id);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);
And selstation_up_btn must be in drawable folder.