英文:
When i want to navigate one activity page to another page my app is crashed in android
问题
This is my MainActivity file
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.abvica.ui.login.LoginActivity
import kotlinx.android.synthetic.main.activity_login2.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Button1.setOnClickListener {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}
}
}
This is AndroidManifest File
<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=".login"></activity>
<activity android:name=".Verify_otp" />
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="369dp"
android:layout_height="41dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="42dp"
android:fontFamily="sans-serif-condensed-light"
android:text=" Welcome To App"
android:textColor="#3888f1"
android:textStyle="bold"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<!-- Other XML elements... -->
<Button
android:id="@+id/Button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/editText5"
android:layout_marginBottom="16dp"
android:background="#3888f1"
android:gravity="center|center_horizontal|center_vertical"
android:text="Get Started"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my code for the button. Please help, I'm new to Android. This is my files and their code. Please tell me where I am going wrong. This is my main activity. I want to connect the LoginActivity
with the main page. Please help.
英文:
This is my MainActivity file
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.abvica.ui.login.LoginActivity
import kotlinx.android.synthetic.main.activity_login2.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Button1.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
}
}
}
this is AndroidManifest File
<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=".login"></activity>
<activity android:name=".Verify_otp" />
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="369dp"
android:layout_height="41dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="42dp"
android:fontFamily="sans-serif-condensed-light"
android:text=" Welcome To App"
android:textColor="#3888f1"
android:textStyle="bold"
android:textSize="30sp"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.297"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<TextView
android:id="@+id/textView2"
android:layout_width="369dp"
android:layout_height="41dp"
android:layout_marginStart="28dp"
android:layout_marginLeft="42dp"
android:layout_below="@+id/textView1"
android:fontFamily="sans-serif-condensed-light"
android:text="Oyyaah! Your account has been varified.
Please update your details and password"
android:textColor="#000"
android:textSize="18sp"
android:typeface="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.111" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/welcome"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="369dp"
android:layout_height="41dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="42dp"
android:fontFamily="sans-serif-condensed-light"
android:text="Enter Name"
android:textColor="#000"
android:textSize="18sp"
android:typeface="normal"
android:layout_below="@+id/imageView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.813" />
<EditText
android:id="@+id/editText5"
style="@style/Widget.AppCompat.EditText"
android:layout_width="260dp"
android:layout_height="41dp"
android:layout_above="@+id/Button1"
android:layout_below="@id/textView3"
android:layout_alignBottom="@id/textView3"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginTop="600dp"
android:layout_marginLeft="47dp"
app:layout_constraintVertical_bias="0.813"
android:ems="10"
android:background="@drawable/custom_input"
android:inputType="textPersonName|text"
android:singleLine="false"
android:hint="@string/type_your_name"
android:paddingLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/Button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/editText5"
android:layout_marginBottom="16dp"
android:background="#3888f1"
android:gravity="center|center_horizontal|center_vertical"
android:text="Get Started"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout
This is my code for button please help am new in android. This is my files and their code please tell me where I am going wrong. This is my main activity. I want to connect .login activity with main page please help
答案1
得分: 1
看起来你还没有初始化按钮。
使用 findViewById(R.id.buttonId)
来初始化按钮。
英文:
Looks like you haven't initialized the button.
Initialize the button with findViewById(R.id.buttonId);
答案2
得分: 0
似乎你正在尝试调用错误的活动。
你在清单中声明了另外两个活动,分别是这个:
<activity android:name=".login"></activity>
以及这个:
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login" />
按钮监听器调用了这个意图:
val intent = Intent(this,login::class.java)
也许你实现了 .ui.login.LoginActivity
,但却在尝试调用错误的活动 (.login)
。
尝试使用这个替代:
val intent = Intent(this,LoginActivity::class.java)
英文:
Seems you a tring to call the wrong activity.
You have declared 2 other activities in Manifest, this:
<activity android:name=".login"></activity>
and this:
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login" />
The button listener calls this intent:
val intent = Intent(this,login::class.java)
Maybe you implemented .ui.login.LoginActivity
and is tring to call the wrong one (.login)
Try this insted:
val intent = Intent(this,LoginActivity::class.java)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论