moving from one activity to another in Android

2019-02-20 12:46发布

The following if my code. Please tell me what I am doing wrong?

 package version.nitt;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import version.nitt.R;
 import android.util.Log;
 import android.content.Intent;

 public class versionActivity extends Activity {
private Runnable runnable;
private static final String TAG="WElcome!";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startscreen);

    runnable= new Runnable(){
        public void run()
        {
            try
            {
                 Log.v(TAG,"Going to sleep...");  
                Thread.sleep(1000);
                 Log.v(TAG,"Waking up from sleep...");  
            }
            catch(Exception e1)
            {
            }
        }
    };
    try
    {
        Thread t=new Thread(null,runnable);
        t.start();
        final Intent i=new Intent(versionActivity.this,secondActivity.class);
        LinearLayout lin =(LinearLayout) findViewById(R.id.ll);
        lin.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                 finish();
                 startActivity(i);
            }

        });

    }
    catch(Exception e)
    {

    }


        Button button = (Button) findViewById(R.id.cls);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
      });




 }
}

I am trying to move from one activity to another if the user clicks anywhere on the screen. The Linear layout is set to clikable. The application gets loaded. But when the onClick is performed for moving on to the next activity, the app is force closed.

Log Cat:

08-05 14:46:37.028: I/Process(1883): Sending signal. PID: 1883 SIG: 9
08-05 14:46:51.479: D/dalvikvm(1915): GC_FOR_ALLOC freed 31K, 4% free 6342K/6595K, paused 64ms
08-05 14:46:51.499: I/dalvikvm-heap(1915): Grow heap (frag case) to 7.279MB for 1080016-byte allocation
08-05 14:46:51.650: D/dalvikvm(1915): GC_FOR_ALLOC freed <1K, 4% free 7396K/7687K, paused 59ms
08-05 14:46:51.819: D/dalvikvm(1915): GC_CONCURRENT freed <1K, 4% free 7396K/7687K, paused 4ms+13ms
08-05 14:46:52.019: D/dalvikvm(1915): GC_FOR_ALLOC freed <1K, 4% free 7396K/7687K, paused 57ms
08-05 14:46:52.031: I/dalvikvm-heap(1915): Grow heap (frag case) to 7.737MB for 480016-byte allocation
08-05 14:46:52.162: D/dalvikvm(1915): GC_FOR_ALLOC freed 0K, 5% free 7865K/8199K, paused 65ms
08-05 14:46:52.439: D/dalvikvm(1915): GC_CONCURRENT freed 1054K, 17% free 6813K/8199K, paused 10ms+5ms
08-05 14:46:52.840: D/dalvikvm(1915): GC_CONCURRENT freed 351K, 15% free 6973K/8199K, paused 8ms+6ms
08-05 14:46:52.840: V/WElcome!(1915): Going to sleep...
08-05 14:46:53.838: V/WElcome!(1915): Waking up from sleep...
08-05 14:47:03.398: D/AndroidRuntime(1915): Shutting down VM
08-05 14:47:03.398: W/dalvikvm(1915): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-05 14:47:03.428: E/AndroidRuntime(1915): FATAL EXCEPTION: main
08-05 14:47:03.428: E/AndroidRuntime(1915): android.content.ActivityNotFoundException: Unable to find explicit activity class {version.nitt/version.nitt.secondActivity}; have you declared this activity in your AndroidManifest.xml?
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1504)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.app.Activity.startActivityForResult(Activity.java:3131)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.app.Activity.startActivity(Activity.java:3237)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at version.nitt.versionActivity$2.onClick(versionActivity.java:46)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.view.View.performClick(View.java:3110)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.view.View$PerformClick.run(View.java:11934)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.os.Handler.handleCallback(Handler.java:587)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.os.Looper.loop(Looper.java:132)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at android.app.ActivityThread.main(ActivityThread.java:4123)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at java.lang.reflect.Method.invoke(Method.java:491)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-05 14:47:03.428: E/AndroidRuntime(1915):     at dalvik.system.NativeStart.main(Native Method)

2条回答
ら.Afraid
2楼-- · 2019-02-20 13:27

use this..simple way of moving one activity to another

        public void onClick(View arg0) {

            Intent intent = new Intent(context, App2Activity.class);
                        startActivity(intent);   

        }

add in manifest.xml

查看更多
ゆ 、 Hurt°
3楼-- · 2019-02-20 13:35
lin.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {

       Intent i = new Intent(versionActivity.this, secondActivity.class);     
       startActivity(i);
       finish(); //should use the finish if you need to preserve memory
                 //other wise don't use it.
    }

});

SECOND STEP: AndroidManifest.xml

<activity  android:name=".secondActivity">
     <intent-filter>
        <action android:name="android.intent.action.MAIN" />                   
     </intent-filter>
</activity>
查看更多
登录 后发表回答