Error in getting value from textviews in getIntent.getParcelableExtra - java

I was working with android project these days, and It will finish soon, but I got stuck in my project. First, I want to create search activity which will be used for searching movies. The searching activity runs well, but whenever it comes to adding the selected movie, it shown me the error code below:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.idstream.pojo.Movies.setTitle(java.lang.String)' on a null object reference
at com.example.idstream.search.MoviesDetailActivity.addFavorite(MoviesDetailActivity.java:96)
at com.example.idstream.search.MoviesDetailActivity.access$100(MoviesDetailActivity.java:19)
at com.example.idstream.search.MoviesDetailActivity$1.onClick(MoviesDetailActivity.java:78)
This code has an error in this area:
Movies movies = getIntent().getParcelableExtra(EXTRA_TITLE);
movies.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
movies.setRelease_info(getIntent().getStringExtra(EXTRA_RELEASE));
movies.setLanguage(getIntent().getStringExtra(EXTRA_LANGUAGE));
movies.setDescription(getIntent().getStringExtra(EXTRA_OVERVIEW));
movies.setPhotos(getIntent().getStringExtra(EXTRA_PHOTOS));
I've tried to add the selected movie to database in other activity, but it didn't mention any error. But when I tried to save the selected movie to database, it turns out an error. I also tried to send it to a toast, which I wanna make sure whether the value that I sent was successfully retrieved. The toast is successfully get the data.
Here is my SearchFragment.java:
public class SearchMovieFragment extends Fragment implements LoaderManager.LoaderCallbacks<ArrayList<Movies>> {
ListView lvMovieItems;
EditText txtTitleMovieInput;
ImageView imgMoviesSearch;
Button btnSearchMovie;
AdaptersMovie adaptersMovie;
MovieHelper movieHelper;
Boolean act = true;
Boolean insert = true;
Boolean delete = true;
private View mView;
public SearchMovieFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_search_movie, container, false);
txtTitleMovieInput = (EditText)mView.findViewById(R.id.txtMovieTitle);
String mTitles = txtTitleMovieInput.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MOVIE, mTitles);
imgMoviesSearch = (ImageView)mView.findViewById(R.id.imgMovies);
btnSearchMovie = (Button) mView.findViewById(R.id.btn_search);
btnSearchMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mTitleMovie = txtTitleMovieInput.getText().toString();
if(TextUtils.isEmpty(mTitleMovie)){
return;
}
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MOVIE, mTitleMovie);
getLoaderManager().restartLoader(0, bundle, SearchMovieFragment.this);
}
});
getLoaderManager().initLoader(0, bundle, SearchMovieFragment.this);
adaptersMovie = new AdaptersMovie(getActivity());
adaptersMovie.notifyDataSetChanged();
lvMovieItems = (ListView)mView.findViewById(R.id.listMovies);
lvMovieItems.setAdapter(adaptersMovie);
lvMovieItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Movies item = (Movies)parent.getItemAtPosition(position);
Intent intent = new Intent(getActivity(), MoviesDetailActivity.class);
intent.putExtra(MoviesDetailActivity.EXTRA_MOVIE, item.getTitle());
intent.putExtra(MoviesDetailActivity.EXTRA_RELEASE, item.getRelease_info());
intent.putExtra(MoviesDetailActivity.EXTRA_LANGUAGE, item.getLanguage());
intent.putExtra(MoviesDetailActivity.EXTRA_OVERVIEW, item.getDescription());
intent.putExtra(MoviesDetailActivity.EXTRA_PHOTOS, item.getPhotos());
startActivity(intent);
}
});
return mView;
}
#NonNull
#Override
public Loader<ArrayList<Movies>> onCreateLoader(int id, #Nullable Bundle args) {
String temp = "";
if (args != null){
temp = args.getString(EXTRA_MOVIE);
}
return new MovieAsyncTaskLoader(getActivity(), temp);
}
#Override
public void onLoadFinished(#NonNull Loader<ArrayList<Movies>> loader, ArrayList<Movies> data) {
adaptersMovie.setData(data);
}
#Override
public void onLoaderReset(#NonNull Loader<ArrayList<Movies>> loader) {
adaptersMovie.setData(null);
}
}
My MoviesDetailActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies_detail);
setTitle("Movie's Details");
tvTitles = findViewById(R.id.movieTitles);
tvReleased = findViewById(R.id.movieRelease);
tvLanguages = findViewById(R.id.movieLanguages);
tvDescription = findViewById(R.id.movieDescriptions);
imageMovies = findViewById(R.id.moviesImage);
progressBar = findViewById(R.id.progressMovie);
fav_moviesBtn = findViewById(R.id.fab_movie);
progressBar.setVisibility(View.VISIBLE);
String mvTitles = getIntent().getStringExtra(EXTRA_MOVIE);
String mvLanguages = getIntent().getStringExtra(EXTRA_LANGUAGE);
String mvOverview = getIntent().getStringExtra(EXTRA_OVERVIEW);
String mvRelease = getIntent().getStringExtra(EXTRA_RELEASE);
String mvPhotos = getIntent().getStringExtra(EXTRA_PHOTOS);
tvTitles.setText(mvTitles);
tvReleased.setText(mvRelease);
tvLanguages.setText(mvLanguages);
tvDescription.setText(mvOverview);
Glide.with(MoviesDetailActivity.this)
.load("https://image.tmdb.org/t/p/w185" + mvPhotos)
.placeholder(R.color.colorFreshOrange)
.dontAnimate()
.into(imageMovies);
movieHelper = new MovieHelper(MoviesDetailActivity.this);
movieHelper.open();
mMoviess = getIntent().getIntExtra(FAVOURITE,0);
fav_moviesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isAdd) {
addFavorite();
Toast.makeText(MoviesDetailActivity.this, "Berhasil Ditambahkan", Toast.LENGTH_LONG).show();
fav_moviesBtn.setImageResource(R.drawable.ic_launcher_fav_yes_24_foreground);
} else {
removeFavorite();
Toast.makeText(MoviesDetailActivity.this, "Berhasil Dihapuskan", Toast.LENGTH_LONG).show();
fav_moviesBtn.setImageResource(R.drawable.ic_launcher_fav_no_24_foreground);
}
}
});
progressBar.setVisibility(View.GONE);
}
private void addFavorite() {
Movies movies = getIntent().getParcelableExtra(EXTRA_TITLE);
movies.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
movies.setRelease_info(getIntent().getStringExtra(EXTRA_RELEASE));
movies.setLanguage(getIntent().getStringExtra(EXTRA_LANGUAGE));
movies.setDescription(getIntent().getStringExtra(EXTRA_OVERVIEW));
movies.setPhotos(getIntent().getStringExtra(EXTRA_PHOTOS));
movieHelper.insertMovie(movies);
}
private void removeFavorite() {
Movies movies = new Movies();
movies.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
movies.setRelease_info(getIntent().getStringExtra(EXTRA_RELEASE));
movies.setLanguage(getIntent().getStringExtra(EXTRA_LANGUAGE));
movies.setDescription(getIntent().getStringExtra(EXTRA_OVERVIEW));
movies.setPhotos(getIntent().getStringExtra(EXTRA_PHOTOS));
movieHelper.deleteMovie(getIntent().getStringExtra(EXTRA_MOVIE));
}
}
And this one is my MovieHelper.java:
public Boolean getOne(String name){
String querySingleRecord = "SELECT * FROM " + DATABASE_TABLE + " WHERE " +DatabaseContract.MovieColoumn.TITLE+ " " + " LIKE " +"'"+name+"'" ;
Cursor cursor = database.rawQuery(querySingleRecord,null);
cursor.moveToFirst();
Log.d("cursor", String.valueOf(cursor.getCount()));
if (cursor.getCount() > 0 ){
return true;
}else if(cursor.getCount() == 0){
return false;
}
return false;
}
public long insertMovie(Movies mMovies){
ContentValues args = new ContentValues();
args.put(IDS,mMovies.getId());
args.put(DatabaseContract.MovieColoumn.TITLE,mMovies.getTitle());
args.put(DatabaseContract.MovieColoumn.RELEASE_INFO,mMovies.getRelease_info());
args.put(DatabaseContract.MovieColoumn.LANGUAGE,mMovies.getLanguage());
args.put(DatabaseContract.MovieColoumn.DESCRIPTION,mMovies.getDescription());
args.put(DatabaseContract.MovieColoumn.PHOTOS,mMovies.getPhotos());
return database.insert(DATABASE_TABLE,null,args);
}

I believe the problem is here:
Movies movies = getIntent().getParcelableExtra(EXTRA_TITLE);
movies.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
Notice that you're using the same EXTRA_TITLE for both.
I think that what's happening is that getParcelableExtra() is returning null, because the implementation of Bundle (the extras) will catch the ClassCastException:
// ...
try {
return (T) o;
} catch (ClassCastException e) {
typeWarning(key, o, "Parcelable", e);
return null;
}
And then you get a NullPointerException when you try to call setTitle().
Instead, create a new Movies instance yourself:
Movies movies = new Movies();
movies.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
Alternatively, you could implement the Parcelable interface in your Movies class, and then you wouldn't have to bother adding all of its fields one by one.

Related

Get data from Search result(in a fragment) and update (other)fragments in the tab layout

I have an Android Activity which has a tab-layout(with four fragments), and first fragment have a search function which search the data through a backend service. When it's success in getting data, then it should update other fragments with relevant data(recycler views and text views). But i cannot get an idea of how to work this thing around. I'll provide what i have done so far, let me know what am i doing wrong and how should i progress. A Code example is more than welcome.
What i have tried so far is within my search fragment, when the search function retrieves success data it will show a list item first of the users profile data. When on click on that item the whole activity will restart and the data is being binded to all the other fragments. What i need to achieve now is without refreshing nor restarting the whole activity, pass the data to relevant fragments and update the views.
Collection Activity is the holder activity for my four fragments of the tab layout
In User Profile / Search Fragment
/**this updates the view from customer adapters data passing**/
public static UserProfileFragment newInstance(CustomerDTO commonCustomerDTOList, ArrayList<ContractDTO> commonContractDTOArrayList) {
UserProfileFragment fragment = new UserProfileFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("CUS_DATA", commonCustomerDTOList);
bundle.putSerializable("LOAN_DATA", commonContractDTOArrayList);
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_profile, container, false);
...
if (getArguments() != null) {
CustomerDTO commonCustomerDTOList = (CustomerDTO) getArguments().getSerializable("CUS_DATA");
setCustomerData(commonCustomerDTOList);
NIC_NUMBER = ((CollectionActivity) Objects.requireNonNull(getActivity())).NIC_NUMBER;
sharedPreferences = getActivity().getSharedPreferences(SharedValue.MYSESSION, Context.MODE_PRIVATE);
}
return view;
}
/** set customer data from search result**/
#SuppressLint("SetTextI18n")
private void setCustomerData(CustomerDTO customerDTO) {
if (customerDTO != null) {
tvNic.setText(customerDTO.getNicNumber());
txtFullName.setText(customerDTO.getTitle() + " " + customerDTO.getInitials() + " " + customerDTO.getLastName());
txtMobile.setText(customerDTO.getMobileNo());
tvAddress1.setText(customerDTO.getAddress().split("%")[0] + ",");
tvAddress2.setText(customerDTO.getAddress().split("%")[1] + ",");
tvAddress3.setText(customerDTO.getAddress().split("%")[2] + "");
}
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_search_sUP:
if (AppStatus.getInstance(Objects.requireNonNull(this.getActivity())).isOnline()) {
/**
* Internet is available, Toast It!
*/
if (sSearchCriterion.equals("NIC (NEW/OLD)")) {
if (Validation.isValidNic(editTextSearch.getText().toString().trim().toUpperCase())) {
} else {
editTextSearch.setError("Enter Valid NIC Number");
return;
}
}
showProgress();
SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences(SharedValue.MYSESSION, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(SharedValue.MYSESSION_NIC_NUMBER, editTextSearch.getText().toString().trim().toUpperCase());
editor.putString(SharedValue.MYSESSION_MODULE_CODE, pProductType);
editor.putString(SharedValue.MYSESSION_SEARCH_TYPE, catNormal);
editor.apply();
boolean isLoad = new Handler().postDelayed(() -> {
String SearchMode = "";
int searchCriteriaType = SelectionCriteriaType.getSearchCriteriaType(sSearchCriterion);
if (searchCriteriaType == 1) {
SearchMode = SelectionCriteriaType.getSearchMode(SelectionCriteriaType.NIC);
} else {
SearchMode = SelectionCriteriaType.getSearchMode(SelectionCriteriaType.OTHER);
}
String CustomerCode = editTextSearch.getText().toString().trim().toUpperCase();
if (!CustomerCode.isEmpty()) {
boolean isValidNic = Validation.isValidNic(CustomerCode);
switch (searchCriteriaType) {
case 1:
if (!sSearchCriterion.equals("NIC (NEW/OLD)")) {
isValidNic = true;
getLoanDetails(pProductType, "1", String.valueOf(searchCriteriaType), CustomerCode);
} else {
getLoanDetails(pProductType, catNormal, String.valueOf(searchCriteriaType), CustomerCode);
}
break;
case 2:
isValidNic = true;
getLoanDetails(pProductType, SearchMode, String.valueOf(searchCriteriaType), CustomerCode);
break;
}
if (isValidNic) {
} else {
hideProgress();
editTextSearch.setError("Enter Valid NIC Number");
}
} else {
hideProgress();
}
hideProgress();
this.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}, 8000);
} else {
/**
* Internet is NOT available, Toast It!
*/
Toasty.error(this.getActivity(), "Please Connect to Internet!", Toast.LENGTH_SHORT).show();
}
break;
}
}
...
private void getLoanDetails(String moduleCode, String searchMode, String searchType, String searchCode) {
System.out.println("call loan details...");
CommonUtils.hideKeyboardFrom(this.getActivity(), view);
ArrayList<ContractDTO> contractVehicleDTOArrayListClear = new ArrayList<>();
ArrayList<CustomerDTO> customerDTOArrayListClear = new ArrayList<>();
CustomerAdapter customerAdapter = new CustomerAdapter(this.getActivity(), customerDTOArrayListClear, contractVehicleDTOArrayListClear, searchCode, moduleCode);
listViewCustomerList.setAdapter(customerAdapter);
customerAdapter.notifyDataSetChanged();
try {
showProgress();
String param59 = moduleCode + "|" + searchMode + "|" + searchType + "|" + searchCode;
new ISOConverter(new RResponce() {
#Override
public void get61and62Response(String res1, String res2) {
if (res1.trim().isEmpty() || res2.trim().isEmpty()) {
CommonAlert.showAlertFailure(getActivity(), "No Details Found...");
search_result_card.setVisibility(View.GONE);
} else {
CustomerDTO customerDTO = null;
if (sSearchCriterion.equals("BR NUMBER")) {
customerDTO = new CustomerDTO(res1, "");
} else {
customerDTO = new CustomerDTO(res1);
}
ArrayList<ContractDTO> contractDTOArrayList = new ContractDTO().getObjectArrayList(res2);
ArrayList<CustomerDTO> customerDTOArrayList = new ArrayList<>();
customerDTOArrayList.add(customerDTO);
CustomerAdapter customerAdapter = new CustomerAdapter(getActivity(), customerDTOArrayList, contractDTOArrayList, searchCode, pProductType);
listViewCustomerList.post(() -> {
listViewCustomerList.setAdapter(customerAdapter);
customerAdapter.notifyDataSetChanged();
});
if (customerAdapter.getCount() > 0) {
hideProgress();
search_result_card.setVisibility(View.VISIBLE);
setCustomerData(customerDTO);
}
hideProgress();
}
}
#Override
public void get39Response(String responseCode) {
System.out.println(responseCode);
}
#Override
public void get63Response(String error) {
System.out.println(error);
if (!error.trim().isEmpty()) {
new ShowMessageAsync(getActivity(), error, ShowMessageAsync.FAILED).execute();
}
}
}).convertToISO(
this.getActivity(),
MTITransaction.GET_CALL,
MTITransaction.MTI_GET,
ISOProcessCode.TRANS_CODE_LOAN_SEARCH,
"null", param59, "");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Customer Adapter
Context mContext;
List<CustomerDTO> customerDTOList;
LayoutInflater inflater;
String NicNumber;
String moduleCode;
ArrayList<ContractDTO> contractDTOArrayList;
UserProfileFragment userProfileFragment;
public CustomerAdapter(Context mContext, List<CustomerDTO> customerDTOList, ArrayList<ContractDTO> contractDTOArrayList, String NicNumber,String moduleCode) {
this.mContext = mContext;
this.customerDTOList = customerDTOList;
this.contractDTOArrayList = contractDTOArrayList;
this.NicNumber = NicNumber;
this.moduleCode = moduleCode;
inflater = LayoutInflater.from(mContext);
}
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
...
holder.imageButton.setOnClickListener(v -> {
Intent intent = new Intent(mContext, CollectionActivity.class);
// Intent intent = new Intent("Pass Searched Data");
// Pass all data arrearsList
// intent.setAction("Pass Searched Data");
intent.putExtra(DataparsingValue.VARIABLE_NIC_NUMBER, NicNumber);
intent.putExtra(DataparsingValue.VARIABLE_CUS_DATA, customerDTOList.get(position));
intent.putExtra(DataparsingValue.VARIABLE_LOAN_DATA, contractDTOArrayList);
intent.putExtra(DataparsingValue.MODULE_CODE, moduleCode);
// intent.putExtra(DataparsingValue.VARIABLE_LOAN_WITH_VEHICLE_DATA, contractVehicleDTOArrayList);
// Pass all data flag
// Start SingleItemView Class
mContext.startActivity(intent);
});
return convertView;
}
DownPayment Fragment
//update the view using
public static DownPaymentFragment newInstance(CustomerDTO commonCustomerDTOList, ArrayList<ContractDTO> commonContractDTOArrayList) {
DownPaymentFragment fragment = new DownPaymentFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("CUS_DATA", commonCustomerDTOList);
bundle.putSerializable(DataparsingValue.VARIABLE_LOAN_WITH_VEHICLE_DATA, commonContractDTOArrayList);
fragment.setArguments(bundle);
return fragment;
}
Receipts Fragment
//Update the list using
public static ReceiptingFragment newInstance(CustomerDTO commonCustomerDTOList, ArrayList<ContractDTO> commonContractDTOArrayList) {
ReceiptingFragment fragment = new ReceiptingFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(DataparsingValue.VARIABLE_CUS_DATA, commonCustomerDTOList);
bundle.putSerializable(DataparsingValue.VARIABLE_LOAN_DATA, commonContractDTOArrayList);
fragment.setArguments(bundle);
return fragment;
}
I expect to Load the data from Search results to the Tab layout's relevant fragments without restarting the Whole activity.

App crashes after theme change (apparently caused by fragment)

My Main Activity has three tabs. Each tab is a fragment. Now if you change the theme (white and dark are available), the activity is being recreated so that the change takes effect. But the app crashes.
How I deal with the fragments:
if (savedInstanceState == null) {
pageadapter = new SectionsPageAdapter(getSupportFragmentManager());
rFragMore = new RoomlistFragmentMore();
rFragMyRooms = new RoomlistFragmentMyRooms();
rFragFavs = new RoomlistFragmentFavorites();
} else {
rFragMyRooms = (RoomlistFragmentMyRooms)pageadapter.getItem(0);
rFragFavs = (RoomlistFragmentFavorites)pageadapter.getItem(1);
rFragMore = (RoomlistFragmentMore)pageadapter.getItem(2);
pageadapter.clearAdapter();
pageadapter = new SectionsPageAdapter(getSupportFragmentManager());
}
How I set up the Adapter:
private void setupViewPager(ViewPager viewPager) {
pageadapter.addFragment(rFragMyRooms, getResources().getString(R.string.myrooms));
pageadapter.addFragment(rFragFavs, getResources().getString(R.string.favorites));
pageadapter.addFragment(rFragMore, getResources().getString(R.string.more));
viewPager.setAdapter(pageadapter);
}
My Adapter:
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void clearAdapter() {
mFragmentList.clear();
mFragmentTitleList.clear();
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
And the Error Log:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileInputStream android.content.Context.openFileInput(java.lang.String)' on a null object reference
at com.yannick.mychatapp.RoomlistFragmentMore.readFromFile(RoomlistFragmentMore.java:246)
at com.yannick.mychatapp.RoomlistFragmentMore.addRoomToList(RoomlistFragmentMore.java:121)
at com.yannick.mychatapp.RoomlistFragmentMore.access$000(RoomlistFragmentMore.java:46)
at com.yannick.mychatapp.RoomlistFragmentMore$1.onDataChange(RoomlistFragmentMore.java:79)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database##16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database##16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database##16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
EDIT: the code of RoomlistFragmentMore
public class RoomlistFragmentMore extends Fragment {
private ListView listView;
private List<HashMap<String, String>> listItems = new ArrayList<>();
private String raumname, theme;
private static String userID = "";
private SimpleAdapter adapter;
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot().child("rooms");
private ArrayList<Room> raumliste = new ArrayList<>();
private TextView keinraumgefunden;
private String[] kat;
private static final String TAG = "RoomlistFragmentMore";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.roomlist_fragment_more,container,false);
listView = view.findViewById(R.id.listView);
keinraumgefunden = view.findViewById(R.id.keinraumgefunden);
kat = getResources().getStringArray(R.array.categories);
theme = readFromFile("mychatapp_theme.txt");
adapter = new SimpleAdapter(getActivity(), listItems, R.layout.listlayout,
new String[]{"name", "kat", "lock", "newest"},
new int[]{R.id.raumname, R.id.raumkat, R.id.raumlock, R.id.raumdatum});
listView.setAdapter(adapter);
root.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
addRoomToList(dataSnapshot);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), R.string.nodatabaseconnection, Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int position = listView.getPositionForView(view);
String roomname = listItems.get(position).values().toArray()[0].toString();
Room room = findRoom(raumliste, roomname);
request_password(room, position);
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
if (raumliste.isEmpty()) {
keinraumgefunden.setText(R.string.noroomfound);
} else {
keinraumgefunden.setText("");
}
}
});
return view;
}
private void addRoomToList(DataSnapshot dataSnapshot) {
HashMap<String, String> raeume = new HashMap<>();
raumliste.clear();
for(DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()){
String name = uniqueKeySnapshot.getKey();
for(DataSnapshot roomSnapshot : uniqueKeySnapshot.getChildren()){
Room room = roomSnapshot.getValue(Room.class);
room.setRaumname(name);
if (!room.getPasswd().equals(readFromFile("mychatapp_raum_" + name + ".txt"))) {
raeume.put(name, kat[Integer.parseInt(room.getCaty())]+"/"+"\uD83D\uDD12"+"/");
raumliste.add(room);
}
break;
}
}
listItems.clear();
Iterator it = raeume.entrySet().iterator();
while (it.hasNext()){
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry)it.next();
resultsMap.put("name", pair.getKey().toString());
String daten = pair.getValue().toString();
String caty = daten.substring(0, daten.indexOf("/"));
String lock = daten.substring(daten.indexOf("/")+1, daten.lastIndexOf("/"));
String time = daten.substring(daten.lastIndexOf("/")+1, daten.length());
String newestTime = "";
int index = 0;
resultsMap.put("kat", caty);
resultsMap.put("lock", lock);
resultsMap.put("newest", newestTime);
if (time.equals("")) {
listItems.add(resultsMap);
} else {
listItems.add(index, resultsMap);
}
}
adapter.notifyDataSetChanged();
}
private void request_password(final Room room, final int position) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.enter_room, null);
raumname = room.getRaumname();
userID = readFromFile("mychatapp_userid.txt");
final EditText input_field = view.findViewById(R.id.room_password);
AlertDialog.Builder builder;
if (theme.equals(getResources().getStringArray(R.array.themes)[1])) {
builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogDark));
} else {
builder = new AlertDialog.Builder(getActivity());
}
builder.setTitle(R.string.pleaseenterpassword);
builder.setView(view);
builder.setCancelable(false);
builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
View view = ((AlertDialog) dialogInterface).getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
dialogInterface.cancel();
}
});
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (input_field.getText().toString().trim().equals(room.getPasswd())) {
Intent tabIntent = new Intent("tab");
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(tabIntent);
Intent intent = new Intent(getActivity(), ChatActivity.class);
intent.putExtra("room_name", room.getRaumname());
intent.putExtra("user_id",userID);
updateRoomList(position);
writeToFile(room.getPasswd(),"mychatapp_raum_" + raumname + ".txt");
alert.cancel();
startActivity(intent);
} else {
Toast.makeText(getActivity(), R.string.wrongpassword, Toast.LENGTH_SHORT).show();
}
}
});
}
});
alert.show();
}
public Room findRoom(ArrayList<Room> raumliste, String raumname) {
for (Room room : raumliste) {
if (room.getRaumname().equals(raumname)) {
return room;
}
}
return null;
}
public void writeToFile(String text, String datei) {
Context context = getActivity();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(datei, Context.MODE_PRIVATE));
outputStreamWriter.write(text);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
public String readFromFile(String datei) {
Context context = getActivity();
String erg = "";
try {
InputStream inputStream = context.openFileInput(datei);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
erg = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return erg;
}
private void updateRoomList(int position) {
listItems.remove(position);
adapter.notifyDataSetChanged();
}
}
The NullPointerException occured while onDataChange() was executed (you can see this by reading the stack trace). More specifically, readFromFile() needs a valid Context to open a file.
Since your app crashed we know that getActivity() did return null. How can this happen?
You add the ValueEventListener in onCreateView(). At this point in time, the Fragment has a valid Context (see the documentation for an explanation of the Lifecycle), so all is well for the moment.
But since you do not remove the ValueEventListener, it will continue to fire even if the Fragment is temporarily not attached to the Activity because the user swiped to the next page. The Fragment won't be garbage collected because you keep it in a list and reuse it.
This approach is basically ok if you implement null checks to avoid accessing the Activity, the Context or Views in general while they are not present. Of course, you could consider a stronger separation of the data and the View layer as suggested in this guide to app architecture

SQLite Update not working in android

I've been trying to update my data in sqlite android, but it doesn't work. Also, there is also no error message shown in the logcat.
Here are my codes:
Dbadapter.java:
public boolean updateEntry(SalesItemInformationLV testing)
{
ContentValues values = new ContentValues();
try {
values.put(ENTRY_NAME, testing.getItemname());
values.put(ENTRY_TEL, testing.getCostprice());
values.put(ENTRY_SELLINGPRICE, testing.getSellingprice());
values.put(ENTRY_QTYSOLD, testing.getItemquantity());
values.put(ENTRY_DATESOLD, testing.getDatesold());
values.put(ENTRY_STAFFDISCOUNT, testing.getStaffdiscount());
}
catch (SQLiteException e)
{
Log.w(MYDBADAPTER_LOG_CAT, "Update failed!" + e.toString());
}
// updating row
int returnvalue = _db.update(DATABASE_TABLE, values, KEY_ID + " = ?", new String[] { String.valueOf(testing.getId()) });
return returnvalue==1 ? true : false;
}
MyItems.java:
public boolean updateDatabase(SalesItemInformationLV testing2, Context c)
{
MyDbAdapter db = new MyDbAdapter(c);
db.open();
boolean rowIDofUpdatedEntry = db.updateEntry(testing2);
db.close();
return rowIDofUpdatedEntry;
}
ListView.java, which passes saleitem2 object intent to Edit.java:
MyItems mi2;
private ArrayList<SalesItemInformationLV> displayiteminfo2;
mi2 = MyItems.getInstance();
displayiteminfo2 = mi2.retrieveAllForlist2(getApplicationContext());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SalesItemInformationLV saleitem2 = displayiteminfo2.get(position);
String namevalue = saleitem2.getItemname();
Intent myintent = new Intent(ListSaleItemActivity.this, EditSaleActivity.class);
myintent.putExtra("array", saleitem2);
startActivity(myintent);
Edit.java which will use the update method from Items.java:
Bundle extras = this.getIntent().getExtras();
final SalesItemInformationLV sale1 = (SalesItemInformationLV)extras.getSerializable("array");
final int id1 = extras.getInt("itemID");
btnSaveChanges.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mc.updateDatabase(sale1, getApplicationContext());
}}
I have been trying to figure out, and I think there could be something wrong in my Dbadapter.java, but i am not sure. Please advise me on how to solve this thanks!

Android Error: IllegalStateException

I am working on a Bitcoin dashboard for Android. The following fragment uses the entered wallet address to display the balance in BTC. When an address is entered, it will add to the listview. When an item in the listview is selected, it will set the edittext to that address.
It is not yet complete, but for now it is throwing an error with the message, "The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread."
I currently have two example addresses in place for testing. If I select one then the other then the first again etc. it works fine. The error appears when I select one, press the button, then select the other.
public class WalletFragment extends Fragment {
ArrayList<String> savedWallets;
ArrayAdapter<String> listAdapter;
String newWalletAddress, jsonString, address, balance;
JSONObject jsonObj, data;
Double balanceDouble;
DecimalFormat df = new DecimalFormat("#.####");
private WalletListener listener;
public interface WalletListener {
void onCreateWallet(String newWalletAddress);
}
public WalletFragment() {
// Required empty public constructor
}
public static WalletFragment newInstance(ArrayList<String> wallets) {
WalletFragment fragment = new WalletFragment();
Bundle args = new Bundle();
args.putStringArrayList("savedWallets", wallets);
fragment.setArguments(args);
return fragment;
}
public static WalletFragment newInstance(ArrayList<String> wallets, String json) {
WalletFragment fragment = new WalletFragment();
Bundle args = new Bundle();
args.putStringArrayList("savedWallets", wallets);
args.putString("jsonString", json);
fragment.setArguments(args);
return fragment;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof WalletListener) {
listener = (WalletListener) context;
}
else {
throw new ClassCastException(context.toString()
+ " must implement MyListFragment.OnItemSelectedListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wallet, container, false);
ListView lv = (ListView) v.findViewById(R.id.walletListView);
df.setRoundingMode(RoundingMode.CEILING);
final EditText walletAddressEditText = (EditText) v.findViewById(R.id.walletAddressEditText);
TextView addressTV = (TextView) v.findViewById(R.id.walletAddresstextView);
TextView balanceTV = (TextView) v.findViewById(R.id.walletBalanceTextView);
savedWallets = getArguments().getStringArrayList("savedWallets");
if (savedWallets == null) {
savedWallets = new ArrayList<>();
}
savedWallets.add("198aMn6ZYAczwrE5NvNTUMyJ5qkfy4g3Hi");
savedWallets.add("1L8meqhMTRpxasdGt8DHSJfscxgHHzvPgk");
// TODO remove test addresses
jsonString = getArguments().getString("jsonString");
if (jsonString != null) {
try {
jsonString = getArguments().getString("jsonString");
jsonObj = new JSONObject(jsonString);
data = new JSONObject(jsonObj.getString("data"));
balance = data.getString("balance");
balanceDouble = Double.parseDouble(balance);
address = data.getString("address");
String walletAddressText = getResources().getString(R.string.wallet_address, address);
addressTV.setText(walletAddressText);
String walletBalanceText = getResources().getString(R.string.wallet_balance, df.format(balanceDouble));
balanceTV.setText(walletBalanceText);
// TODO add viewing for other wallet data at some point
} catch (Exception e) {
Log.d("TickerException", e.toString());
}
}
listAdapter = new ArrayAdapter<>(getActivity(), R.layout.main_list_rows, savedWallets);
lv.setAdapter(listAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String address = savedWallets.get(position);
Log.d("wallet", "Selected: " + address);
walletAddressEditText.setText(address);
}
});
Button button = (Button) v.findViewById(R.id.createWalletButton);
View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View view) {
newWalletAddress = walletAddressEditText.getText().toString();
if (walletAddressEntryStructuralValidation(newWalletAddress)) {
if (newWalletAddress != null) {
listener.onCreateWallet(newWalletAddress);
}
else {
Toast.makeText(getActivity(), "newWalletAddress is null", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(getActivity(), "Please enter a valid wallet address (length is currently " + newWalletAddress.length() + ").", Toast.LENGTH_SHORT).show();
}
}
};
// TODO check if wallet is already on list
button.setOnClickListener(ocl);
return v;
}
public boolean walletAddressEntryStructuralValidation(String address) {
return ((address.length() > 25) &&
(address.length() < 36) && (
(address.substring(0,1).equals("1") ||
(address.substring(0,1).equals("3")))));
}
// Wallet addresses are 26-35 alphanumeric characters and begin with 1 or 3
}
I believe this is all the relevant code but I will be closely watching this thread if anyone needs to request additional source.
That message means that the contents of the adapter (the order of items you see in getItem) changed but notifyDataSetChanged or similar function wasn't called. When changing the items in your adapter contents (which in this case is the savedWallets array list) you must call one of those functions.
Note: If you're adding several objects at once, you only need to call it once after all are added/removed. If you're mutating an object but not adding/removing it, you do not need to call it, but calling it may be the easiest way of doing a redraw.

How to make dynamic API calls depending on user preferences?

I am using theMovieDb.com API to fetch data.On launching the app it shows grid view of popular movies. In setting menu, it has option to sort the grid view on the basis of more popular and highest rated. I am using shared preferences to store user preferences and a listener on preference change.But unable to sort the grid view.
MainActivityFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_movies);
final ImageAdapter mAdapter= new ImageAdapter(getActivity(),listMovie);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Movie movieSelected= listMovie.get(position);
String movieTitle=movieSelected.getTitle();
String movieThumbnail=movieSelected.getUrlThumbnail();
String movieReleaseDate=movieSelected.getReleaseDate();
String movieOverview=movieSelected.getOverview();
String movieRating = movieSelected.getRating();
Intent i = new Intent( getActivity() ,DetailsMovies.class);
i.putExtra(EXTRA_MESSAGE_1, movieThumbnail);
i.putExtra(EXTRA_MESSAGE_2, movieTitle);
i.putExtra(EXTRA_MESSAGE_3, movieReleaseDate);
i.putExtra(EXTRA_MESSAGE_4, movieRating);
i.putExtra(EXTRA_MESSAGE_5, movieOverview);
startActivity(i);
}
});
return rootView;
}
private void sendJsonRequest() {
//In the case of theMovieDB it is JSON Object Request
//Specify several argument in JSON Object Request
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
getRequestUrl(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
parseJsonResponse(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private void parseJsonResponse(JSONObject response) {
if (response == null || response.length() == 0) {
return;
}
long id = -1;
String title = Constants.NA;
String releaseDate = Constants.NA;
String synopsis = Constants.NA;
String urlThumbnail = Constants.NA;
String rating = Constants.NA;
try {
if (response.has(KEY_RESULTS)) {
JSONArray jsonArray = response.getJSONArray(KEY_RESULTS);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject currentMovies = jsonArray.getJSONObject(i);
//Get the id of the current movie
//If statement is used to check whether id is null or not.
if (currentMovies.has(KEY_ID) && !currentMovies.isNull(KEY_ID)) {
id = currentMovies.getLong(KEY_ID);
}
//Get the synopsis of the current movie
if (currentMovies.has(KEY_OVERVIEW) && !currentMovies.isNull(KEY_OVERVIEW)) {
synopsis = currentMovies.getString(KEY_OVERVIEW);
}
//Get the title of the current movie
if (currentMovies.has(KEY_TITLE) && !currentMovies.isNull(KEY_TITLE)) {
title = currentMovies.getString(KEY_TITLE);
}
//Get the urlThumbnail of the current movie
if (currentMovies.has(KEY_POSTER_PATH) && !currentMovies.isNull(KEY_POSTER_PATH)) {
urlThumbnail = currentMovies.getString(KEY_POSTER_PATH);
}
//Get the release date of the current movie
if (currentMovies.has(KEY_RELEASE_DATE) && !currentMovies.isNull(KEY_RELEASE_DATE)) {
releaseDate = currentMovies.getString(KEY_RELEASE_DATE);
}
//Get the rating of current movie
if (currentMovies.has(KEY_VOTE_AVERAGE) && !currentMovies.isNull(KEY_VOTE_AVERAGE)) {
rating = currentMovies.getString(KEY_VOTE_AVERAGE);
}
//Create movie object
movie=new Movie();
movie.setId(id);
movie.setTitle(title);
movie.setUrlThumbnail("http://image.tmdb.org/t/p/w185/" + urlThumbnail);
movie.setReleaseDate(releaseDate);
movie.setOverview(synopsis);
movie.setRating(rating);
//This decides when to add movies to the ArrayList
if (id != -1 && !title.equals(Constants.NA)) {
listMovie.add(movie);
}
}
}
} catch (JSONException e) {
Log.e("error", e.getMessage());
}
}
public String getRequestUrl() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
String order = pref.getString(getString(R.string.pref_order_key), getString(R.string.pref_popularity));
if(order.equals(getString(R.string.pref_rating)))
return URL + "vote_average" + UrlEndpoints.URL_PARAM + MyApplication.API_KEY;
else
return URL+ "popularity"+UrlEndpoints.URL_PARAM + MyApplication.API_KEY;
}
//Base Adapter which is used to put poster in grid view
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Movie> movieItems;
public ImageAdapter(Context c, ArrayList<Movie> movieList) {
this.mContext = c;
this.movieItems = movieList;
}
public int getCount() {
return movieItems.size();
}
public Object getItem(int position) {
return movieItems.get(position);
}
public long getItemId(int position) {
return position;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_movies, null);
}
mNetworkImageView = (NetworkImageView) convertView.findViewById
(R.id.networkImageView);
//Getting movie data for the row
Movie m = movieItems.get(position);
//Thumbnail Image
//ImageLoader is used to load the images from json object retrieved.
imageLoader=VolleySingleton.getInstance().getImageLoader();
mNetworkImageView.setImageUrl(m.getUrlThumbnail(), imageLoader);
return convertView;
}
}
}
SettingsActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
// Added preferences from XML
addPreferencesFromResource(R.xml.pref_general);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
// Added preferences
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_order_key)));
}
/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}

Categories

Resources