So, I'm debugging my project in Android Studio, it's just a tutorial I'm doing, and I have a Handler I'm trying to use.
I can step over all the code in the run part. Then when I get out of run it steps into Handler.java and android studio has all these errors marked in in the Handler.java file and the program crashes.
I'm pretty sure Handler
is part of the jdk and when added Handler
to my activity it imported the file automatically. I've invalidated caches and cleaned the project. I've ran into this problem with a number of tutorials but never found an answer on how to solve.
In the other cases I uninstalled and reinstalled android studio but that didn't help me out.
This is the code that eventually steps into Handler.java but that is not the problem. The problem is that Android Studio says there are errors throughout Handler.java. I'm using Android Studio 2.0 but I've run into this with other versions too with other jdk java files.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Cursor c = sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF", null);
Log.d("arindam", "c count"+ c.getCount());
if (c.getCount() == 0){
sqLite.execSQL("INSERT INTO USER_PREF (CITY_NAME, VOICE_ON, NOTIF)" +
" VALUES('NONE', 'Y', 'Y')");
}
c.close();
Cursor d = sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF", null);
Log.d("arindam", "d count" + d.getCount());
if (d.moveToFirst()){
Log.d("arindam", "d NONE" + d.getString(0));
if (d.getString(0).equals("NONE")){
Intent intent = new Intent(StartScreen.this, CityScreen.class);
startActivity(intent);
}
else {
//Intent intent = new Intent(StartScreen.this, HomeScreen.this);
//startActivity(intent);
}
d.close();
finish();
}
}
},1000);
This is the logcat.
11-24 22:05:47.270 1740-1740/com.example.andrewspiteri.basket E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrewspiteri.basket, PID: 1740
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andrewspiteri.basket/com.example.andrewspiteri.basket.CityScreen}: java.lang.RuntimeException: native typeface cannot be made
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:175)
at android.graphics.Typeface.createFromAsset(Typeface.java:149)
at com.example.andrewspiteri.basket.CityScreen.onCreate(CityScreen.java:26)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
This is CityScreen.java, the program doesn't even get there.
public class CityScreen extends ActionBarActivity {
SQLiteDatabase sqLite;
Spinner city_spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_screen);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/books.TTF");
//section is to hide the action bar.
ActionBar actionBar = getActionBar();
actionBar.hide();
//Ideally SQL should be handled in a separate helper,
//but for ease of understanding to start
//off, I have kept the code here.
sqLite = this.openOrCreateDatabase("basketbuddy",MODE_PRIVATE, null);
Cursor c = sqLite.rawQuery("SELECT CITY_NAME FROM CITY_LIST",null);
//ideally at least 1 city should be there in city_name
//As I have already synced this with the serve in
//StartScreen.java
if (c.getCount() == 0){
Toast.makeText(getApplicationContext(), "Oh ho..." +
"Some unexpected problem. Please restart the application",
Toast.LENGTH_LONG);
}
TextView city_selection = (TextView) findViewById(R.id.SelectCityText);
city_selection.setTypeface(type);
//Defining the array that will hold the City Names
String[] city_name_db = new String[(c.getCount()+1)];
//By default, the first entry for city list is "Choose City"
//We will understand who this is necessary later.
city_name_db[0] = "Choose City";
//Moving the city names from sqlite to an array city_name_db
if (c.moveToFirst()){
int count = 1;
do {
city_name_db[count] = c.getString(0);
count++;
}
while (c.moveToNext());{
}
//creating an ArrayAdapter for the spinner and then
//associating the ArrayAdapter to the spinner
ArrayAdapter<String> aa = new ArrayAdapter<String>
(getApplicationContext(),R.layout.spinner_item,city_name_db);
city_spinner = (Spinner) findViewById(R.id.spinner1);
city_spinner.setAdapter(aa);
//There is an inherent problem with Spinners. Lets
//assume that there are 3 cities Delhi, Gurgaon, Noida.
//The moment I populate these 3 cities to the spinner,
//by default Delhi will get selected as this is the first
//entry. OnItemSelectedListener will get triggered
//immediately with Delhi as selection and the code will
//proceed. Net net, even the default first value is
//taken as an ItemSelected trigger. The way to bypass
//this is to add a default value 'Choose City' in the
// ArrayAdapter list. Then inside the onItemSelected method,
//ignore if 'Choose City' has been selected.
//SetOnItemSelectedListener listens for any change in item
//if found then it will call onItemSelectedListener listens
//listens for any change in item selected, if found
//then it will call onItemSelected method.
city_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView
parent, View view,
int position, long id) {
if (parent.getItemAtPosition(position).equals("Choose City")){
//do nothing
}
else {
//save selected city as a default city
//for shopping. This city selection is saved in DB
//We may even decide to send to send this data to server,
// however in this example, we are not doing so.
String city_name = city_spinner.getSelectedItem().toString();
Cursor c = sqLite.rawQuery("SELECT CITY_NAME FROM USER_PREF",
null);
if (c.getCount() == 0){sqLite.execSQL("insert into USER_PREF"+"" +
"(CITY_NAME, VOICE_ON) VALUES ('" + city_name +
"', 'Y', 'Y')");
}
if (c.moveToFirst()){
sqLite.execSQL("update USER_PREF set CITY_NAME = '" +
city_name + "'");
}
//Intent intent = new Intent(CityScreen.this, HomeScreen.class);
//startActivity(intent);
sqLite.close();
finish();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}