Ralph
Ralph

Reputation: 13

Proceeding to another activity by clicking TextView

I want to click the textView so it can go to the second class activity

public void click(View v){
        Intent intent;
        switch(v.getId()){
            case R.id.titleExpense:
                intent = new Intent(this,userExpense.class);
                break;
            case R.id.titleIncome:
                intent = new Intent(this,userIncome.class);
                break;

        }
        startActivity(intent);
    }

The error I have is the intent startActivity(intent), I'm not entirely sure where I'm suppose to declare it.

Layout: (Same for the Income one)

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/titleExpense"
        android:id="@+id/titleExpense"
        android:textSize="20sp"
        android:clickable="true"
        android:onClick="onClick"
        android:layout_below="@+id/txtGroup"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

From the manifest file:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".userExpense" />
        <activity android:name=".userIncome"></activity>
    </application>

Upvotes: 0

Views: 2964

Answers (7)

OneCricketeer
OneCricketeer

Reputation: 191874

According to

android:onClick="onClick"

Your method needs to be named onClick, not click.

public void onClick(View v){
    Intent intent = null;
    switch(v.getId()){
        case R.id.titleExpense:
            intent = new Intent(this,userExpense.class);
            break;
        case R.id.titleIncome:
            intent = new Intent(this,userIncome.class);
            break;

    }
    if (null!=intent) startActivity(intent);
  }

Or, use the Java code to setOnClickListener into the textview.

Upvotes: 2

xbadal
xbadal

Reputation: 1375

Try This i.e make Intent Global;

 Intent intent;
@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.text:
            intent = new Intent(this, Main2Activity.class);
            break;
    }
    startActivity(intent);
}

Upvotes: 0

Umesh
Umesh

Reputation: 1

Try this one.

public void click(View v){
    Intent intent;
    switch(v.getId()){
        case R.id.titleExpense:
            intent = new Intent(MainActivity.this,userExpense.class);
            startActivity(intent);
            break;
        case R.id.titleIncome:
            intent = new Intent(MainActivity.this,userIncome.class);
            startActivity(intent);
            break;
        }   
}

Upvotes: 0

SaravInfern
SaravInfern

Reputation: 3388

Have you implemented the onclickListener and intialized for the views in oncreate

Sample

public class MyActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_LAYOUT);

        Button titleExpense= (Button) findViewById(R.id.titleExpense);
        titleExpense.setOnClickListener(this);

        Button titleIncome = (Button) findViewById(R.id.titleIncome);
        titleIncome.setOnClickListener(this);


    }
@Override
public void onClick(View v){
        Intent intent;
        switch(v.getId()){
            case R.id.titleExpense:
                intent = new Intent(this,userExpense.class);
                startActivity(intent);
                break;
            case R.id.titleIncome:
                intent = new Intent(this,userIncome.class);
                startActivity(intent);
                break;
            }   
    }

Upvotes: 0

Sean83
Sean83

Reputation: 493

You should modify your xml

For example,

<TextView
    android:id="@+id/tv1"
    android:layout_width=blahblah
    android:layout_height=blahblah
    android:onClick="onClick"
    android:clickable="true" /> 

and in java file,

tv1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    Intent intent;
    switch(v.getId()){
        case R.id.titleExpense:
            intent = new Intent(this,userExpense.class);
            startActivity(intent);
            break;
        case R.id.titleIncome:
            intent = new Intent(this,userIncome.class);
            startActivity(intent);
            break;
        }   
    }
});

Upvotes: 0

Rajan Kali
Rajan Kali

Reputation: 12953

When you are referring to 'this' , it refers to instance of View you clicked, you can use getApplicationContext() to refer to parent Activity and always use default with switch case to avoid null errors ,try below code

        public void click(View v){
                    Intent intent;
                    switch(v.getId()){
                        case R.id.titleExpense:
                            intent = new Intent(getApplicationContext(),userExpense.class);
                            break;
                        case R.id.titleIncome:
                        default :
                            intent = new Intent(getApplicationContext(),userIncome.class);
                            break;

                    }
                    startActivity(intent);
          }

Upvotes: 0

Mujammil Ahamed
Mujammil Ahamed

Reputation: 1494

Try this one,

public void click(View v){
        Intent intent;
        switch(v.getId()){
            case R.id.titleExpense:
                intent = new Intent(this,userExpense.class);
                startActivity(intent);
                break;
            case R.id.titleIncome:
                intent = new Intent(this,userIncome.class);
                startActivity(intent);
                break;
            }   
    }

Upvotes: 0

Related Questions