如何在Android Studio中将类添加到AndroidManifest.xml文件?

时间:2014-08-24 17:44:02

标签: java android xml android-studio

我从eclipse切换到android studio,我正在尝试向我的清单文件添加一个活动,我不知道我应该使用的代码,我的应用程序一直在崩溃继承我的清单中的代码

<?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>
            <action android:name="android.intent.action.MAIN" />

            <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时。下面是我用来发送意图的代码

Settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent GoToSettings = new Intent(getApplicationContext(), settings.class);

            startActivity(GoToSettings);
            finish();
        }
    });

继承我的logcat

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)

这是整个设置类

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();
                 }




             }
    }

_______ UPDATE ______ 我发现了问题!

我不得不改变我宣布按钮变换的方式

 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);

}

2 个答案:

答案 0 :(得分:1)

从类名的外观来看,它应该如下所示,大小写非常重要。

<activity android:name=".Settings"
          android:label="Pay Day!!">

</activity>

<activity android:name=".Statistics"
          android:label="Pay Day!!">

</activity>

答案 1 :(得分:0)

在清单中声明<uses-sdk android:minSdkVersion="minimum_integer_value" android:targetSdkVersion="your_target_integer_value" />。可能是您尝试使用较高级别的API

在较低版本上运行它