简体   繁体   中英

how to code activity page… android

I am new to android and I am learning from tutorials. What I am trying to do is link 2 xml pages. After login go to content page. The problem is when I add intent code in my class file the emulator provides the error msg "the app has stopped working" else the code is working perfectly.

This is the intent code I have tried:

Intent myIntent = new Intent(v.getContext(), secondpage.class);
startActivityForResult(myIntent, 1); 

I also tried this

Intent myIntent = new Intent(MainActivity.this, secondpage.class);
startActivity(myIntent); 

and last, this

Intent myIntent = new Intent(getApplicationContext(), secondpage.class);
startActivity(myIntent); 

my secondpage.class code is given below (the name of the xml file is cf)

public class secondpage extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cf);
    }
}

What am I doing wrong? Why does the intent cause the emulator to flash the error msg?

my manifest.xml

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.qwedd.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>
</application>

my log cat

02-10 03:07:49.440: D/dalvikvm(903): Not late-enabling CheckJNI (already on)
02-10 03:07:52.290: D/dalvikvm(903): GC_FOR_ALLOC freed 54K, 5% free 2890K/3016K, paused 39ms, total 41ms
02-10 03:07:52.370: I/dalvikvm-heap(903): Grow heap (frag case) to 12.512MB for 10092616-byte allocation
02-10 03:07:52.440: D/dalvikvm(903): GC_FOR_ALLOC freed 2K, 2% free 12744K/12876K, paused 65ms, total 65ms
02-10 03:07:54.010: D/(903): HostConnection::get() New Host Connection established 0xb81424b8, tid 903
02-10 03:07:54.080: W/EGL_emulation(903): eglSurfaceAttrib not implemented
02-10 03:07:54.090: D/OpenGLRenderer(903): Enabling debug mode 0
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903): KeyEvent: ACTION_UP but key was not down.
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903):   in android.widget.EditText{b3d9ea80 VFED..CL .F...... 15,315-465,374 #7f080001 app:id/txtPassword}
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903):   0: sent at 103941000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=103941, downTime=103804, deviceId=0, source=0x101 }
02-10 03:08:38.556: D/AndroidRuntime(903): Shutting down VM
02-10 03:08:38.556: W/dalvikvm(903): threadid=1: thread exiting with uncaught exception (group=0xb3aeaba8)
02-10 03:08:38.566: E/AndroidRuntime(903): FATAL EXCEPTION: main
02-10 03:08:38.566: E/AndroidRuntime(903): Process: com.example.qwedd, PID: 903
02-10 03:08:38.566: E/AndroidRuntime(903): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.qwedd/com.example.qwedd.secondpage}; have you declared this activity in your AndroidManifest.xml?
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Activity.startActivityForResult(Activity.java:3424)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Activity.startActivityForResult(Activity.java:3385)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Activity.startActivity(Activity.java:3627)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.Activity.startActivity(Activity.java:3595)
02-10 03:08:38.566: E/AndroidRuntime(903):  at com.example.qwedd.MainActivity$1.onClick(MainActivity.java:40)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.view.View.performClick(View.java:4438)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.view.View$PerformClick.run(View.java:18422)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.os.Handler.handleCallback(Handler.java:733)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.os.Handler.dispatchMessage(Handler.java:95)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.os.Looper.loop(Looper.java:136)
02-10 03:08:38.566: E/AndroidRuntime(903):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-10 03:08:38.566: E/AndroidRuntime(903):  at java.lang.reflect.Method.invokeNative(Native Method)
02-10 03:08:38.566: E/AndroidRuntime(903):  at java.lang.reflect.Method.invoke(Method.java:515)
02-10 03:08:38.566: E/AndroidRuntime(903):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-10 03:08:38.566: E/AndroidRuntime(903):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-10 03:08:38.566: E/AndroidRuntime(903):  at dalvik.system.NativeStart.main(Native Method)
02-10 03:09:23.546: I/Process(903): Sending signal. PID: 903 SIG: 9

Generally speaking, Every Activity needs it's own xml layout file (unless using more advanced methods, probably that is not your intention anyhow).

in the onCreate method you see the setContentView(R.layout.cf); - meaning this activity is calling the layout xml called "cf".

So, if you want two activities and you want to go from one to other - do the following:

1) in the first activity, in the onCreate method, call your first layout file like so:

 public class firstActivity extends Activity{

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.firstlayout);        

}

In your second activity, in the onCreate method, call your second layout file like so:

public class secondActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlayout);        

    }

If you have more than one activity, you need to declare them in your manifest.xml for example:

<activity android:name=".secondPage" >    </activity>

Now, in your first Activity, you will want to go to second activity usually when you press a button or something. so, in your onClickListener (or wherever you want to go to second page), you call an Intent like so:

Intent intent = new Intent(firstPage.this, secondPage.class);
startActivity(intent);

That should do the trick!

I think you are missing to add activity to your Manifest file. If it is so then check your manifest file that you have declared seconpage activity in manifest file. Or if it is not added then add it as below.

add it as below.

 <activity android:name=".seconpage" >
        </activity>

add this line after completing your first activity tag.

you are getting android.content.ActivityNotFoundException

Your app didn't able to find secondpage Activity in your app. so first you have to register every Activity to your manifest file before using them.

<activity android:name=".secondpage"></activity>

and for calling this you can use

Intent myIntent = new Intent(MainActivity.this, secondpage.class);
            startActivity(myIntent); 

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