Type preference activity is dispatched - android-activity

protected void onCreate (Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
addPreferencesFromResources(R.xml.prefs);
}

protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
addPreferencesFromResource (R.xml.YourFileName);
}

Related

Huawei Map suddenly unable to load and crash

The map was working right before suddenly started to crash on activity open.
The code was same as working and no changes made. what coul be the problem?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView = findViewById(R.id.map);
checkPermission();
if (permission) {
mapView.getMapAsync(this);
mapView.onCreate(savedInstanceState);
}
}
#Override protected void onStart() { super.onStart(); mMapView.onStart(); }
#Override protected void onResume() { super.onResume(); mMapView.onResume(); }
#Override protected void onPause() { super.onPause(); mMapView.onPause(); }
#Override protected void onStop() { super.onStop(); mMapView.onStop(); }
#Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); }
#Override public void onLowMemory() { super.onLowMemory(); mMapView.onLowMemory(); }
When the code did not change, it could be that the permission of app changed.
perhaps it was reinstalled and uninstalled?
looking at the code, the oncreate is called after permission check.
we need to either ask for permission again, or have the app proceed without map. one of two choices.
This should prevent NullPointerException.
also for permission request, we need to handle SecurityException to avoid crashing.

NullPointerException when notifyDataSetChanged() on ArrayAdapter (ListView in ViewPager)

I have an Activity with ViewPager which manage 2 Fragments using FragmentStatePagerAdapter,
The fragments have ListViews, each of the ListView shows data and I am able to go from Fragment_A to Fragment_B by swipe or item tapping on the ListView of Fragment_A.
I am also able to update the underlying data source for Fragment_B based on the item selected in Fragment_A. But when I try to notifyDataSetChanged() on the DataAdapter of Fragment_B, I am getting NullPointerException .
I want to update the data in Fragment_B according to the item selected in Fragment_A.
Here is my code
public class MyActivity extends FragmentActivity implements AdapterView.OnItemClickListener{
ViewPager viewPager = null;
int mSelectedItemPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_fail_activity);
viewPager = (ViewPager) findViewById(R.id.fail_pager_VP);
viewPager.setAdapter(new pagerAdapter(getSupportFragmentManager()));
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedItemPosition = position;
viewPager.setCurrentItem(1);
FragmentStatePagerAdapter fspa = (FragmentStatePagerAdapter) viewPager.getAdapter();
Fragment_B item = (Fragment_B) fspa.getItem(1);
item.setData(position);
item.refresh();
}
}
class pagerAdapter extends FragmentStatePagerAdapter {
public pagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new Fragment_A();
} else if (position == 1) {
fragment = new Fragment_B();
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String title = "";
if (position == 0) {
return "a_title";
}else if (position == 1) {
return "b_title";
}
}
}
public class Fragment_A extends android.support.v4.app.Fragment {
private ListView frag_a_listView;
DataAdapter dataAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView frag_a_listView = (ListView) getActivity().findViewById(R.id.frag_a_LV);
frag_a_listView.setOnItemClickListener((AdapterView.OnItemClickListener) getActivity());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_fail_main, container, false);
frag_a_listView = (ListView) layout.findViewById(R.id.frag_a_LV);
dataAdapter = new DataAdapter(getActivity(),
R.layout.data_row,
R.id.data_label_TV,
getFrag_aData());
frag_a_listView.setAdapter(dataAdapter);
return layout;
}
private List<String> getFrag_aData() {
...
return someData;
}
}
public class Fragment_B extends android.support.v4.app.Fragment {
ListView frag_b_listView;
List<String> mData;
DataAdapter dataAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView frag_b_listView = (ListView) getActivity().findViewById(R.id.frag_b_LV);
frag_b_listView.setOnItemClickListener((AdapterView.OnItemClickListener) getActivity());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_frag_b, container, false);
frag_b_listView = (ListView) layout.findViewById(R.id.frag_b_LV);
setData(0);
dataAdapter = new DataAdapter(getActivity(),
R.layout.data_row,
R.id.data_label_TV,
mData);
frag_b_listView.setAdapter(dataAdapter);
return layout;
}
public void setData(int position) {
...
mData = some List<String>;
}
public void refresh() {
dataAdapter.notifyDataSetChanged(); <------- this crashes the app
}
}
public class DataAdapter extends ArrayAdapter {
public DataAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
return convertView;
}
}
here is the crash log
06-25 16:51:46.813 21537-21537/au.myCity.eight E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: au.myCity.eight, PID: 21537
java.lang.NullPointerException: Attempt to invoke virtual method 'void au.myCity.eight.dataAdapter.notifyDataSetChanged()' on a null object reference
at au.myCity.eight.fragments.Fragment_B.refresh(Fragment_B.java:56)
at au.myCity.eight.MyActivity.onItemClick(MyActivity.java:37)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
After few tries with Log.d and checking some variable references.
The problem was the reference to Fragment_B changed by the time I called dataAdapter.notifyDataSetChanged(), to fix this I used the solution suggested in SO here, thanks to the "Streets Of Boston".
Summery, In the PagerAdapter:
Save a reference to initialized fragments in a SparseArray, use getRegisteredFragment in
onItemClick instead of getItem in MyActivity.
Override instantiateItem and destroyItem as suggested in the link.

Start an instance of activity

How can i start an instance of activity.
My custom activity has a constructor take some paramters.
public class CustomActivity extends Activity {
public CustomActivity(String someParameters) {
}
}
And i create an instance of it. How to start it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomActivity activity = new CustomActivity("TEST");
// how to start to the above activity from this activity
}

getting parse initialization error while unit testing activity with Robolectrics framework and eclipse

I am unit testing activity with Robolectric framework and getting the following error of parse initialization error.
MyActivity.java
public class WelcomeActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// hide action bar
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.hide();
//Checking Current User
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null)
{
Intent i=new Intent(WelcomeActivity.this, ViewPagerStyleLandingActivity.class);
startActivity(i);
WelcomeActivity.this.finish();
}
setContentView(R.layout.actvity_welcome);
SharedPreferences pref = getSharedPreferences(Constants.SIGNUP_CHECK_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.PREF_KEY_FIRST_RUN, false);
editor.commit();
Button loginbut=(Button) findViewById(R.id.login);
Button registerbut=(Button) findViewById(R.id.register);
loginbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(i);
}
});
registerbut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i=new Intent(WelcomeActivity.this,RegisterActivity.class);
startActivity(i);
}
});
}
}
and have made the test case as
#RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
private WelcomeActivity activity;
private Button loginbut;
#Before
public void setup() {
activity = Robolectric.buildActivity(WelcomeActivity.class).create().get();
loginbut = (Button) activity.findViewById(R.id.button);`}
#Test
public void checkActivityNotNull() throws Exception {
assertNotNull(activity);
}
#Test
public void shouldHaveButtonThatSaysAudit() throws Exception{
assertThat((String) loginbut.getText(), equalTo("Login"));
}
#Test
public void pressingLaunchButtonForSecondActivity() throws Exception {
String resultValue = "Testing Text";
assertNotNull(loginbut);
loginbut.performClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
assertThat(resultValue, equalTo(startedIntent.getStringExtra("result")));
}
private ShadowContextWrapper shadowOf(WelcomeActivity activity2) {
return null;
}
#After
public void tearDown() throws Exception {
// Nothing Here
}
}
now getting the error
when running with jUnit

android Call ListActivity from activity class

I have layouts main activity and another list activity. But when i try to call list activity i am getting crash. Please help me to start list activity (Note: List activity has EditText and button to add text to existing listview in list Activity)
All i wanted to add text listview which is already exist in listactivity.
Many Thanks!!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
EditText txt=(EditText)findViewById(R.id.txtPass);
if (txt.getText().length()==0){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
return;
}
if(txt.getText().toString().contains("Test")){
Button btn;
btn=(Button)findViewById(R.id.btnSubmit);
Intent intent=new Intent(this,Passwordlist.class);
startActivity(intent);
}
}
First, make some change to your code:
Button btn;
EditText txt
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Do this stuff here and not in onClick. Your views should be defined in //onCreate() and before the onCLick()
txt=(EditText)findViewById(R.id.txtPass);
btn=(Button)findViewById(R.id.btnSubmit);
}
public void onClick(View view){
Intent intent=new Intent(YourActivity.this,Passwordlist.class);
if (txt.getText().toString().matches("")){
Toast.makeText(this, "Please enter password to continue!!", Toast.LENGTH_SHORT).show();
txt.getText().clear();
}
else if(txt.getText().toString().contains("Test")){
startActivity(intent);
}
}
See if this works.