简体   繁体   中英

Intent constructor arguments undefined

I want to start an activity, so I picked this Class way. I know I can do it easily by passing ACTION NAME directly in intent constructor but I am learning android so I trying to learn new ways. I started another activity with the same code in other class and it working there but here it is giving the following error. I think it has to some thing with THREAD class.

The error is:

The constructor Intent(new Thread(){}, Class) is undefined. 

How can I solve this? Here is the code:

package com.umer.practice2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SplashScreen extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    Thread timer= new Thread()
    {
        public void run()
        {
        try
        {
            sleep(5000);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            Class ourclass = Class.forName("com.umer.practice2.StartingPoint");
            Intent myintent= new Intent(this,ourclass); // error
            startActivity(myintent);
        }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

}

Here is the layout of the class activity which is giving force close error.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="hint"
        android:id="@+id/fdisp" 
        />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="How many times you eat in a day"
        android:gravity="center"
        android:id="@+id/but3" 
        />


</LinearLayout>

and here is the code of the class itself

    package com.umer.practice2;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class MyClass extends Activity{

    Button b3;
    TextView disp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         b3= (Button) findViewById(R.id.but3);
         disp= (TextView) findViewById(R.id.fdisp);

         b3.setOnClickListener(new View.OnClickListener() {

             @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                    disp.setText("It depends ");
            }
        });
    }
    }

Just replace the intent creation by:

Intent myintent= new Intent(SplashScreen.this, StartingPoint.class);

Otherwise, this refers to the enclosing Thread class

you can call as given below

startActivity(new Intent(SplashScreen.class, StartingPoint.class);

It will work. Try it.

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