0

So I know it has to do with my "startActivity( myIntent )" line of code. Here is my first Activity file

public class Commander2 extends Activity {
    /** Called when the activity is first created. */

    private static final String TAG = "AccessCodeEntry";
    private static final String LEVEL_ONE_ACCESS_CODE = "derp";

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.accesscodeentry );
    }

protected void onDestroy() {
    super.onDestroy();
}

public void accessCodeEntered( View v ) {
    EditText loginPassword = (EditText) findViewById( R.id.editText1 );
    if( loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE ) ) {
        Intent myIntent = new Intent( Commander2.this, LevelOne.class );
        startActivity( myIntent );
    } else {
        Intent myIntent = new Intent( Commander2.this, LevelZero.class );
        startActivity( myIntent );
    }
}
}

Here is the XML file as well.

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="accessCodeEntered"
        android:text="OK" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="19dp"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignLeft="@+id/editText1"
        android:text="Enter Access Code, or leave blank for Level 0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

My code just exits as soon as I enter a password and press the OK button, no matter what the output. I have commented out the startActivity( myIntent ) lines and the code finishes without crashing. Does anyone know what I'm doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="my.eti"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity android:name=".Commander2"
              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=".LevelOne"
              android:label="@string/app_name">
        <intent-filter>                
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LevelZero"
              android:label="@string/app_name">
        <intent-filter>                
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>
JuiCe
  • 4,132
  • 16
  • 68
  • 119

4 Answers4

1

Please post your logcat errors, but my guess is to make sure that LevelOne and LevelZero are in your manifest file. Also, it might help to change this:

loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )

to:

loginPassword.getText().toString().equals( LEVEL_ONE_ACCESS_CODE )
Sam
  • 86,580
  • 20
  • 181
  • 179
1

have you to register LevelOne and LevelZero in manifest file

MAC
  • 15,799
  • 8
  • 54
  • 95
1

Here is a link to a similar SO question that shows how to add the activities to your manifest.

Add a new activity to the AndroidManifest?

Community
  • 1
  • 1
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • Well, I did not have it in my manifest, so thanks, but now it is giving me another error: "No launcher activity found! The launch will only sync the application package on the device!" This is my manifest file: – JuiCe Jun 06 '12 at 17:55
  • 1
    Are you still getting that error? (Looks like you shouldn't be by looking at your manifest) Or are you just getting 'unable to resolve target'? If the latter, go to your project properties and change the build target. – Ryan Jun 06 '12 at 20:31
1

EDIT : i am updating my answer

so finally i got where you were going wrong

  • check your if condition
  • in accessCodeEntered() method
  • of Commander2.java Activity

 if(loginPassword.toString().equals(LEVEL_ONE_ACCESS_CODE)){}

here you are not getting the value of password and try to use it so i added "getText()" method and its working fine like this


if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){}

Commander2.java


package my.eti;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Commander2 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    private static final String TAG = "AccessCodeEntry";
    private static final String LEVEL_ONE_ACCESS_CODE = "derp";

    private Button btnOK;
    private EditText passTxt;

    private String strPass;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accesscodeentry);

        passTxt = (EditText) findViewById(R.id.editText1);

        btnOK =(Button) findViewById(R.id.button1);
        btnOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v==btnOK){

            /*... here is the problem we havent added getText() to get value from EditText...*/
        if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){
            Intent myIntent = new Intent( Commander2.this, LevelOne.class );
            Toast.makeText(getBaseContext(), "if condition is true..", Toast.LENGTH_SHORT).show();
            startActivity( myIntent );
        }
        else{
            Intent myIntent = new Intent( Commander2.this, LevelZero.class );
            Toast.makeText(getBaseContext(), "else condition is true..", Toast.LENGTH_SHORT).show();

            startActivity(myIntent);
        }
        }

    }
}

try to implement this code and where you going wrong.

still you get any trouble share with me Thanks.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • it is still crashing unfortunately, I did change the manifest file to what you had, changing the SDK version to 10. – JuiCe Jun 06 '12 at 18:02
  • 1
    make sure you should also changed in "default.properties" file on this line "target=android-8" to 10. – swiftBoy Jun 06 '12 at 18:07
  • Ok I didn't do that. I am changing it now, and all it keeps saying is "unable to resolve target 'android-10.'" Am I not allowed to change which SDK I'm using after creating the project initially? I have only downloaded the most recent SDK on my computer but am trying to code for version 2.3. So in my file it actually says android-15. – JuiCe Jun 06 '12 at 18:16
  • 1
    yes you can change it, now only i have changed my app API 10 to 8, well i have seen your posted manifest and i am wondering why not you have this " to your activities " – swiftBoy Jun 06 '12 at 18:24
  • Ok, I have updated the manifest posted above to what I currently have. The program runs, but it still crashes as soon as I press the OK button from the Commander2 Activity, regardless of what is in the textfield. Also, it still gives me the error 'Unable to resolve target 'android-10'' whenever I change it from andoid-15 – JuiCe Jun 06 '12 at 18:28
  • 1
    after changing all these things make sure clean you project in this way [select project --> go to 'Project' menu--> click on 'Clean' option --> then again run app] – swiftBoy Jun 06 '12 at 18:34
  • I did, unfortunately it still crashes. In my properties file it still says version 15 though, I think that must be the problem – JuiCe Jun 06 '12 at 18:38
  • I tried entering your code, and the program just crashes before it opens. I think it must have something to do with the version I am using. I made a completely new project and copy and pasted all of your code from here, but still no luck. Also, I cannot open the project.properties file in the new project, I'm not sure what to do. Thanks a lot for all of your help though – JuiCe Jun 07 '12 at 12:03
  • 1
    i have sent you updated source code, check mail, what i got is you did mistake at if condition >> in accessCodeEntered() method >>of Commander2.java Activity .... add getText() method before toString to loginPassword ... did you get ? – swiftBoy Jun 07 '12 at 12:16
  • I did get it, it all works, thank you you really helped out a lot – JuiCe Jun 07 '12 at 13:22