简体   繁体   中英

Set Two different layouts

I am trying to put 2 different layouts in my android application and that on value in register using shared preferences this is my code .. it stopped once i click logo

Part of

 protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
            SharedPreferences prefs;
        //String register;
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        register=prefs.getString("register", "no");

         if(register.equalsIgnoreCase("no"))    {

                setContentView(R.layout.main_activity);
                                        }
          else 
           {  
                setContentView(R.layout.about);
           }
        }

the LogCat error

FATAL EXCEPTION: main
Process:com,cpcs,irissystem, PID:31749
java.lang.RuntimeExeption : unable to start activity componentInfo
{......MainActivity }:java.lang.NullPointerException
at android.app.ActivityThread.preformLaunchActivity(ActivityThread.java2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java2386)
At android.app.ActivityThread.access$900(ActivityThread.java:169)
At android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
At android.os.Handler.dispatchMeesage(Handler.java:102)
At android.os.looper.loop(LooperLjava:136)
At android.app.ActivityThread.Main(ActivityThread.java:5476)
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....:1268)

Activity Class

public class MainActivity extends Activity {


public static String register;
private static final int CAMERA_REQUEST = 1888;  
public static String IrisCode;
String IrisCode2;
ImageView imageView;  
private static  Bitmap eyeImage;
EditText passwordText;
public static float perc ;





SharedPreferences prefs;

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   prefs = PreferenceManager.getDefaultSharedPreferences(this);
   register=prefs.getString("register", "no");
   if(register.equalsIgnoreCase("no"))    {
       setContentView(R.layout.main_activity);
   }
   else
  {
      setContentView(R.layout.about);
   }








    Toast.makeText(MainActivity.this,"befroe:  "+ register, Toast.LENGTH_LONG).show();


    Button InstructionButton = (Button) findViewById(R.id.inst);
    Button enrollButton = (Button) findViewById(R.id.enroll);
    Button verifyButton = (Button) findViewById(R.id.ver);
    passwordText = (EditText) findViewById(R.id.pass);

      if(register.equalsIgnoreCase("no")){
          verifyButton.setEnabled(false);
      }

    enrollButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //Go to the next page
            //if Not registered before go to start to enter data
            //else go to setting 
            Intent i = new Intent();
              if(register.equalsIgnoreCase("no"))   {
                  i = new Intent (MainActivity.this,start.class);
                  startActivity(i);                     
                                                    }


              else 
               {
                  i = new Intent (MainActivity.this,setting.class);
                  startActivity(i);
               }

        }
    });

    InstructionButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //Go to the instruction page
            Intent i = new Intent (MainActivity.this,instruction.class);
            startActivity(i);                       
                                                    }

    });






    verifyButton.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View arg0) {

            if("0000".equals(passwordText.getText().toString())){
                String same="aothorizes acess";
                Toast.makeText(MainActivity.this, same, Toast.LENGTH_LONG).show();
                Intent i = new Intent (MainActivity.this,setting.class);
                startActivity(i);

                }


        }
        });




}

Manifest

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.cpcs.irissystem.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=".start"  />
     <activity
        android:name=".setting"  />
     <activity
        android:name=".aboutus"  />
     <activity
        android:name=".instruction"  />

     <activity
        android:name=".setting2"/>
   <activity
        android:name=".ver"/>


</application>
// Try this way,hope this will help you...

SharedPreferences prefs;
String register;
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   prefs = PreferenceManager.getDefaultSharedPreferences(this);
   register=prefs.getString("register", "no");
   if(register.equalsIgnoreCase("no"))    {
       setContentView(R.layout.activity_main);
   }
   else
  {
      setContentView(R.layout.activity_main2);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM