我为我们的学校项目启动了一个应用程序,当我差不多完成时,我在genymotion模拟器上执行了该程序并获得了此代码:
package com.example.user.timetableapp;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
Button btn1,btn2,btn3;
boolean f;
Intent go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f=fileExistance("day1.txt");
String text="";
String s;
btn1= (Button) findViewById(R.id.btn1);
btn2= (Button) findViewById(R.id.btn2);
btn3= (Button) findViewById(R.id.btn3);
if(f==false)
for(int i=1;i<7;i++) {
try {
s="day"+i+".txt";
FileOutputStream fos = openFileOutput(s, MODE_PRIVATE);
try {
fos.write(text.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public boolean fileExistance(String fname){
File file = getBaseContext().getFileStreamPath(fname);
return file.exists();
}
public void go(View v){
go=new Intent(this,ChooseDay.class);
startActivity(go);
}
public void credits(View v){
go=new Intent(this,Credits.class);
startActivity(go);
}
public void howto(View v){
go=new Intent(this,Instructions.class);
startActivity(go);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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);
}
}
这就是错误:
04-08 09:38:02.561 1724-1724/com.example.user.timetableapp I/art﹕ Late-enabling -Xcheck:jni
04-08 09:38:02.871 1724-1724/com.example.user.timetableapp D/AndroidRuntime﹕ Shutting down VM
04-08 09:38:02.874 1724-1724/com.example.user.timetableapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.timetableapp, PID: 1724
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.timetableapp/com.example.user.timetableapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.example.user.timetableapp.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
可能是什么解决方案?我是初学者,所以我不明白这些错误是什么意思。 另外,为什么android studio认为我的手机的API在19时为1? 我有什么方法可以解决这个问题吗?
答案 0 :(得分:1)
检查 AndroidManifest.xml 行android:theme
答案 1 :(得分:1)
您尝试使用无法找到的主题。您可以使用以下行将其添加到Android Manifest文件中。
机器人:主题=&#34; @风格/ Theme.AppCompat.Light&#34;