How to add a class to AndroidManifest.xml file in Android Studio?
本问题已经有最佳答案,请猛点这里访问。
我从Eclipse切换到Android Studio,我正在尝试将一个活动添加到我的清单文件中,我不知道应该使用什么代码,我的应用程序不断崩溃,这是我清单中的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="penis.jason.payday"> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="settings" android:label="Pay Day!!"> </activity> <activity android:name="statistics" android:label="Pay Day!!"> </activity> </application> |
我让它做的是当用户按下设置按钮时,它会把它带到这个类和新的XML中。这是我用来发送意图的代码
1 2 3 4 5 6 7 8 9 |
这是我的落袋猫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 08-24 10:41:52.098 21750-21750/penis.jason.payday E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: penis.jason.payday, PID: 21750 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{penis.jason.payday/penis.jason.payday.settings}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) at android.app.ActivityThread.access$900(ActivityThread.java:169) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5487) 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:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.app.Activity.findViewById(Activity.java:1986) at penis.jason.payday.settings.<init>(settings.java:23) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250) |
这是整个设置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | package penis.jason.payday; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.CheckBox; import java.util.Calendar; @SuppressWarnings("unused") public class settings extends Activity { boolean FullScreen; Button SaveAndExit = (Button) findViewById(R.id.SaveAndExit); Button Save = (Button) findViewById(R.id.Save); Button Cls = (Button) findViewById(R.id.Cls); CheckBox FullScreenOnOff = (CheckBox) findViewById(R.id.fullScreen); CheckBox SaveWarningOnOff = (CheckBox) findViewById(R.id.SaveWarningOnOff); Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month= cal.get(Calendar.MONTH)+1; int day = cal.get(Calendar.DATE); String FullDate = (""+month+"/"+day+"/"+year); String Date=(String.valueOf(FullDate)); @Override protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.settings); FullScreen = getSharedPreferences("Settings",MODE_PRIVATE).getBoolean("FullScreen",false); if(FullScreen==true){ this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.settings); } SaveAndExit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent GoToMain = new Intent(getApplicationContext(), MainActivity.class); startActivity(GoToMain); finish(); } }); Save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); Cls.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); if(FullScreenOnOff.isChecked()){ FullScreen = true; SharedPreferences FullScreenSave = getSharedPreferences("Settings", Context.MODE_PRIVATE); SharedPreferences.Editor FullScreenE = FullScreenSave.edit(); FullScreenE.putBoolean("FullScreen", FullScreen); FullScreenE.commit(); } } } |
____更新____我发现了问题!
我不得不改变我的按钮变量声明方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | boolean FullScreen; Button SaveAndExit,Save,Cls; CheckBox FullScreenOnOff,SaveWarningOnOff; Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month= cal.get(Calendar.MONTH)+1; int day = cal.get(Calendar.DATE); String FullDate = (""+month+"/"+day+"/"+year); String Date=(String.valueOf(FullDate)); @Override protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.settings); FullScreen = getSharedPreferences("Settings",MODE_PRIVATE).getBoolean("FullScreen",false); if(FullScreen==true){ this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.settings); } SaveAndExit = (Button) findViewById(R.id.SaveAndExit); Save = (Button) findViewById(R.id.Save); Cls = (Button) findViewById(R.id.Cls); FullScreenOnOff = (CheckBox) findViewById(R.id.fullScreen); SaveWarningOnOff = (CheckBox) findViewById(R.id.SaveWarningOnOff); |
}
从类名的外观来看,它应该如下所示,大小写很重要。
1 2 3 4 5 6 7 8 9 | <activity android:name=".Settings" android:label="Pay Day!!"> </activity> <activity android:name=".Statistics" android:label="Pay Day!!"> </activity> |
在您的舱单上申报