I want a TextView to respond to click and open a second Activity.The Main Activity contains two text fields, a button and another TextView, which needs to act as a button.This code results in crashing of the App whenever it is run.
Here is the MainActivity.java file.-
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t1 = (TextView)findViewById(R.id.textView2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onSignUpClick(View view){
t1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(".SignUpActivity");
startActivity(intent);
}
}
);
}
}
Here is the AndroidManifest.xml file.-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abinas.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUpActivity"
android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>
What's the error? Try changin your Intent for this
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(intent);
Change the Intent
to something with this structure:
new Intent( current class.this, target activity.class)
In the code it should look like:
public void onSignUpClick(View view){
t1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SignUpActivity.class)
startActivity(intent);
}
}
);
Assuming you are calling onSignUpClick from you XML it should be as simple as:
public void onSignUpClick(View view){
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(intent);
}
Alternatively you could just create the listener when activity is created:
public class MainActivity extends AppCompatActivity {
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.textView2);
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(intent);
}
}
);
}
}
All the view widgets can override onclick method, but you are doing wrong you are using onclick method inside of another onclick method, and another thing is you should use findViewById inside onCreate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1 = (TextView)findViewById(R.id.textView2);
t1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,SignUpActivity.class);
startActivity(intent);
}
}
);
}
}
Move this code inside your onCreate
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.textView2);
t1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(".SignUpActivity");
startActivity(intent);
}
}
);
}
You need to wrap you text view inside a linear layout and make the linear layout clickable.
<LinearLayout
android:id="@+id/linear_forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_small"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintEnd_toEndOf="@+id/btn_enter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_enter">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_forgot_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
tools:text="Forgot password?"
/>
</LinearLayout>
An then get the onclick event in you activity or fragment.
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.