FATAL EXCEPTION: main on Android 4.x but not Android 2.X - java

I am getting some errors when trying to run my android app on an emulator that is running any version of Android 4.x but it works fine on any version of Android 2.X.
package com.EXCLUDED_FOR_PRIVACY.EXCLUDED_FOR_PRIVACY;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.ImageView;
import android.graphics.Color;
public class EXCLUDED_FOR_PRIVACY extends Activity {
public int WebView1Bool=0;
public int WebView2Bool=0;
public int WebView3Bool=0;
public int WebView4Bool=0;
private TabHost tabs;
private void setupTabHost() {
tabs = (TabHost) findViewById(R.id.my_tabhost);
tabs.setup();
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupTabHost();
tabs.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
setupTab(new ImageView(this), 1);
setupTab(new ImageView(this), 2);
setupTab(new ImageView(this), 3);
setupTab(new ImageView(this), 4);
WebView mWebView1;
mWebView1 = (WebView) findViewById(R.id.webview1);
mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView1.setBackgroundColor(0);
mWebView1.getSettings().setJavaScriptEnabled(true);
mWebView1.setWebViewClient(new MobileClient());
mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView1Bool=1;
tabs.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(0);
if(WebView1Bool==0){
WebView mWebView1;
mWebView1 = (WebView) findViewById(R.id.webview1);
mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView1.setBackgroundColor(0);
mWebView1.getSettings().setJavaScriptEnabled(true);
mWebView1.setWebViewClient(new MobileClient());
mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView1Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(2).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(1);
if(WebView2Bool==0){
WebView mWebView2;
mWebView2 = (WebView) findViewById(R.id.webview2);
mWebView2.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView2.setBackgroundColor(0);
mWebView2.getSettings().setJavaScriptEnabled(true);
mWebView2.setWebViewClient(new MobileClient());
mWebView2.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView2Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(4).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(2);
if(WebView3Bool==0){
WebView mWebView3 = (WebView) findViewById(R.id.webview3);
mWebView3.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView3.setBackgroundColor(0);
mWebView3.getSettings().setJavaScriptEnabled(true);
mWebView3.setWebViewClient(new MobileClient());
mWebView3.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView3Bool=1;
}
}
});
tabs.getTabWidget().getChildAt(6).setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
tabs.setCurrentTab(3);
if(WebView4Bool==0){
WebView mWebView4;
mWebView4 = (WebView) findViewById(R.id.webview4);
mWebView4.setBackgroundColor(Color.parseColor("#a3a3a3"));
mWebView4.setBackgroundColor(0);
mWebView4.getSettings().setJavaScriptEnabled(true);
mWebView4.setWebViewClient(new MobileClient());
mWebView4.loadUrl("EXCLUDED_FOR_PRIVACY");
WebView4Bool=1;
}
}
});
}
private class MobileClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
private void setupTab(final View view, final int i) {
View createTabView_view = LayoutInflater.from(tabs.getContext()).inflate(R.layout.tabs_bg, null);
ImageView createTabView_tv = (ImageView) createTabView_view.findViewById(R.id.tabsImage);
if(i==1){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_discounts);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview1);
tabs.addTab(tabSpec);
} else if(i==2){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_specials);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview2);
tabs.addTab(tabSpec);
} else if(i==3){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_directory);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview3);
tabs.addTab(tabSpec);
} else if(i==4){
TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
createTabView_tv.setImageResource(R.drawable.ic_membership);
tabSpec.setIndicator(createTabView_view);
tabSpec.setContent(R.id.webview4);
tabs.addTab(tabSpec);
}
}
}
Here are the errors I am seeing
09-24 13:11:25.329: E/Trace(1151): error opening trace file: No such file or directory (2)
09-24 13:11:25.539: D/dalvikvm(1151): GC_FOR_ALLOC freed 43K, 4% free 8316K/8583K, paused 42ms, total 45ms
09-24 13:11:25.549: I/dalvikvm-heap(1151): Grow heap (frag case) to 8.829MB for 691216-byte allocation
09-24 13:11:25.669: D/dalvikvm(1151): GC_CONCURRENT freed <1K, 4% free 8990K/9287K, paused 75ms+6ms, total 124ms
09-24 13:11:25.829: D/gralloc_goldfish(1151): Emulator without GPU emulation detected.
09-24 13:11:26.209: I/Choreographer(1151): Skipped 42 frames! The application may be doing too much work on its main thread.
09-24 13:11:26.379: I/Choreographer(1151): Skipped 44 frames! The application may be doing too much work on its main thread.
09-24 13:11:27.509: I/Choreographer(1151): Skipped 54 frames! The application may be doing too much work on its main thread.
09-24 13:11:28.319: D/AndroidRuntime(1151): Shutting down VM
09-24 13:11:28.319: W/dalvikvm(1151): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-24 13:11:28.379: E/AndroidRuntime(1151): FATAL EXCEPTION: main
09-24 13:11:28.379: E/AndroidRuntime(1151): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.EXCLUDED.EXCLUDED/com.EXCLUDED.EXCLUDED.EXCLUDED}: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.os.Looper.loop(Looper.java:137)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-24 13:11:28.379: E/AndroidRuntime(1151): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 13:11:28.379: E/AndroidRuntime(1151): at dalvik.system.NativeStart.main(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151): Caused by: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151): at com.EXCLUDED.EXCLUDED.EXCLUDED.onCreate(EXCLUDED.java:85)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.Activity.performCreate(Activity.java:5008)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-24 13:11:28.379: E/AndroidRuntime(1151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-24 13:11:28.379: E/AndroidRuntime(1151): ... 11 more
09-24 13:11:30.289: I/Process(1151): Sending signal. PID: 1151 SIG: 9
I apologize if this is something really simple that any java developer would know, I have to admit I barely know what I am doing in Java but am under the gun to get this built. Thank you very much for any help.

Get rid of all the tabs.getTabWidget().getChildAt(...).setOnClickListener() crap, replacing it with a single TabHost.OnTabChangeListener that you register with your TabHost, to find out when the user changes tabs. Your existing code assumes a particular internal structure of a TabWidget, and that assumption is unreliable, as you are discovering.

Related

Unable to start activity ComponentInfo{Activity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment

I am developing an application which uses google map APIs. In my application i want to show google map and set marker on my current location by getting my device latitude and longitude from GPS. In beginning when i was implement following code that works well, i test it on my device but after installing 15 or 20 times on my mobile device it starts crashing automatically although i didn't do anything on that activity.
The logs which are showing me android studio are following
Logs
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: FATAL EXCEPTION: main
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Process: com.example.nabeelfarooq.mapsnavapplication, PID: 9110
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabeelfarooq.mapsnavapplication/com.example.nabeelfarooq.mapsnavapplication.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:162)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:189)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5529)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at com.example.nabeelfarooq.mapsnavapplication.MainActivity.onCreate(MainActivity.java:59)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5966)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:162) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:189) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5529) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
09-24 16:30:43.414 9110-9110/com.example.nabeelfarooq.mapsnavapplication E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to get length of null array
MainActivity
import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.Console;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener, GoogleApiClient.OnConnectionFailedListener {
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
public static double latitude;
public static double longitude;
private Button button;
private LatlngAdapter databasedata = null;
final int REQUEST_CODE_ASK_PERMISSIONS = 123;
#TargetApi(Build.VERSION_CODES.M)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
i.putExtra("latitude", latitude);
i.putExtra("longitude", longitude);
startActivity(i);
}
});
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if (status != ConnectionResult.SUCCESS) {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
} else {
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = fm.getMap();
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE_ASK_PERMISSIONS);
return;
}
else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
}
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
buildGoogleApiClient();
onMapReady(mMap);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
supportMapFragment.getMapAsync(this);
}
public void turnGPSOn() {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
alertDialogBuilder
.setMessage("GPS is disabled in your device. Enable it?")
.setCancelable(false)
.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
/** Here it's leading to GPS setting options*/
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onPause(){
super.onPause();
mGoogleApiClient.disconnect();
}
//obtain the supportMapFragment and get notified when the map is ready to be used
protected synchronized void buildGoogleApiClient(){
if(mGoogleApiClient==null){
mGoogleApiClient=new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fragmentManager=getFragmentManager();
android.support.v4.app.FragmentManager fragmentManager1=getSupportFragmentManager();
// Handle navigation view item clicks here.
int id = item.getItemId();
if(supportMapFragment.isAdded()) {
fragmentManager1.beginTransaction().hide(supportMapFragment).commit();
}
if (id == R.id.nav_home) {
//fragmentManager1.beginTransaction().add(R.id.map,supportMapFragment).commit();
// Handle the home action
} else if (id == R.id.nav_about_us) {
Intent intent=new Intent(this,About_us_Activity.class);
startActivity(intent);
} else if (id == R.id.nav_contact_us){
Intent intent=new Intent(this,Contact_us_Activity.class);
startActivity(intent);
} else if (id == R.id.nav_Rate_app) {
Intent intent=new Intent(this,TipsActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap =googleMap;
LatLng lahore=new LatLng(latitude,longitude);
//mMap.addMarker(new MarkerOptions().title("lahore").snippet("lahore HQ").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(sydney));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
#Override
public void onMyLocationChange(Location location) {
latitude=location.getLatitude();
longitude=location.getLongitude();
LatLng latLng=new LatLng(latitude,longitude);
//mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
LatLng lahore=new LatLng(latitude,longitude);
//mMap.addMarker(new MarkerOptions().title("You are here").snippet("lahore").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).position(lahore));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(lahore));
}
public void onConnected(Bundle bundle){
Log.d("CONNECTED", "I am connected");
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation!=null){
Log.d("Latitude",String.valueOf(mLastLocation.getLatitude()));
Log.d("Longitude",String.valueOf(mLastLocation.getLongitude()));
}
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
static String latString =Double.toString(latitude);
static String lonString =Double.toString(longitude);
}
content_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:layout_alignParentBottom="true"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="#layout/abc_action_bar_up_container"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Me Somewhere"
android:id="#+id/button"
android:background="#5368ed"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
/>
</RelativeLayout>
EDIT: A user has pointed that this bug is happening also in 7.0 devices, so keep in mind that you may change android:maxSdkVersion="22" to an appropiate Sdk version.
The problem is only happening in 5.1.1 devices with custom roms like Resurrection Remix. I don't know the reason, but you can solve it adding this to your manifest, as seen in https://stackoverflow.com/a/39696819/4208583.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
Remove action bar in manifest by specifying NoActionBar theme

ClassCastException when using Fragment in Android

I've recently studyed android by myself.
and I tried Using Fragment by following my Textbook
but I met an error even though I typed codes exactly the same as my textbook.
Can you guys explain to me what is wrong?
This is MainActivity.java
package com.example.ex_1;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class CounterFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root = inflater.inflate(R.layout.counterfragment, container, false);
Button btnIncrease = (Button)root.findViewById(R.id.btnincrease);
final TextView textCounter = (TextView)root.findViewById(R.id.txtcounter);
btnIncrease.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int count = Integer.parseInt(textCounter.getText().toString());
textCounter.setText(Integer.toString(count + 1));
}
});
return root;
}
}
}
This is activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~~"/>
<fragment
android:name="com.example.ex_1.MainActivity$CounterFragment"
android:id="#+id/counterfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And the error is
09-24 07:40:08.329: E/AndroidRuntime(1794): FATAL EXCEPTION: main
09-24 07:40:08.329: E/AndroidRuntime(1794): Process: com.example.ex_1, PID: 1794
09-24 07:40:08.329: E/AndroidRuntime(1794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex_1/com.example.ex_1.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread.access$800(ActivityThread.java:144)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.os.Handler.dispatchMessage(Handler.java:102)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.os.Looper.loop(Looper.java:135)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread.main(ActivityThread.java:5221)
09-24 07:40:08.329: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Native Method)
09-24 07:40:08.329: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Method.java:372)
09-24 07:40:08.329: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
09-24 07:40:08.329: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
09-24 07:40:08.329: E/AndroidRuntime(1794): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Activity.setContentView(Activity.java:2144)
09-24 07:40:08.329: E/AndroidRuntime(1794): at com.example.ex_1.MainActivity.onCreate(MainActivity.java:19)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Activity.performCreate(Activity.java:5937)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
09-24 07:40:08.329: E/AndroidRuntime(1794): ... 10 more
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.ex_1.MainActivity$CounterFragment that is not a Fragment
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Fragment.instantiate(Fragment.java:606)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Fragment.instantiate(Fragment.java:582)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.app.Activity.onCreateView(Activity.java:5286)
09-24 07:40:08.329: E/AndroidRuntime(1794): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
09-24 07:40:08.329: E/AndroidRuntime(1794): ... 20 more
09-24 07:40:08.329: E/AndroidRuntime(1794): Caused by: java.lang.ClassCastException
09-24 07:40:08.329: E/AndroidRuntime(1794): ... 25 more
problem is that you set your api level too low, so it use "support" version of fragment, instead of "base" fragment
put "min api level" at 11 in your androidmanifest, remove dependency to "support" library, remove "import android.support.v4.app.Fragment" and replace it with "import android.app.fragment" and it should work
change
public class MainActivity extends Activity {
to
public class MainActivity extends FragmentActivity {

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

RadioGroup Crash

I have some problems with my simple app in Android with Java code. I'm trying to set a RadioGroup that works like settings for color of buttons. When I start my app in Settings activity (Settings.java), it crashes.
package com.app.testing;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Settings extends Main implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.redbtn :
add.setBackgroundColor(21);
break;
case R.id.blubtn :
add.setBackgroundColor(58);
break;
case R.id.grebtn :
add.setBackgroundColor(13);
break;
}
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
}
}
Log:
05-27 16:27:49.611: E/AndroidRuntime(4970): FATAL EXCEPTION: main
05-27 16:27:49.611: E/AndroidRuntime(4970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.testing/com.app.testing.Settings}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.os.Looper.loop(Looper.java:137)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-27 16:27:49.611: E/AndroidRuntime(4970): at dalvik.system.NativeStart.main(Native Method)
05-27 16:27:49.611: E/AndroidRuntime(4970): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.RadioGroup
05-27 16:27:49.611: E/AndroidRuntime(4970): at com.app.testing.Settings.onCreate(Settings.java:16)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Activity.performCreate(Activity.java:5104)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-27 16:27:49.611: E/AndroidRuntime(4970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-27 16:27:49.611: E/AndroidRuntime(4970): ... 11 more
Thanks
It seems the problem is in this line
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.ButtonSettingsView);
May be you are trying to cast a TextView to RadioGroup. Check your xml. I am afraid that your id ButtonSettingsView is a textView
I think the LogCat says it all:
Check your XML layout file: is ButtonSettingsView actually a RadioGroup?

check if checkbox is checked return null pointer

I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>

Categories