所以,我在Android Studio中调试我的项目,这只是我正在做的一个教程,我有一个我正在尝试使用的Handler。
我可以跳过运行部分中的所有代码。然后,当我退出运行时,它进入Handler.java并且android studio在Handler.java文件中标记了所有这些错误并且程序崩溃。
我很确定Handler
是jdk的一部分,当我将Handler
添加到我的活动时,它会自动导入文件。我已经使缓存失效并清理了项目。我通过一些教程遇到了这个问题,但从未找到如何解决的答案。
在其他情况下,我卸载并重新安装了android studio,但这并没有帮助我。
这是最终进入Handler.java的代码,但这不是问题。问题是Android Studio说整个Handler.java都有错误。我使用的是Android Studio 2.0,但我和其他jdk java文件一起使用其他版本。
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);
这是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)
这是CityScreen.java,该程序甚至没有到达那里。
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) {
}
});
}
}
答案 0 :(得分:1)
抱歉,我没有足够的声誉发表评论,所以我在这里发帖。
你的问题不是来自Handler,而是:
&#34; java.lang.RuntimeException:原生字体无法制作&#34; 在com.example.andrewspiteri.basket.CityScreen.onCreate( CityScreen.java:26 )
我是你的客人
#Defaults requiretty
nagios ALL=(ALL) NOPASSWD:/usr/lib64/nagios/plugins/check_puppetagent
您可以在Issue when using a custom font - "native typeface cannot be made"或"Native typeface cannot be made" only for some people
中查看答案