When i want to navigate one activity page to another page my app is crashed in android

huangapple go评论61阅读模式
英文:

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

   &lt;application
     android:allowBackup=&quot;true&quot;
     android:icon=&quot;@mipmap/ic_launcher&quot;
     android:label=&quot;@string/app_name&quot;
     android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
     android:supportsRtl=&quot;true&quot;
     android:theme=&quot;@style/AppTheme&quot;&gt;
     &lt;activity android:name=&quot;.login&quot;&gt;&lt;/activity&gt;
     &lt;activity android:name=&quot;.Verify_otp&quot; /&gt;
     &lt;activity
        android:name=&quot;.ui.login.LoginActivity&quot;
        android:label=&quot;@string/title_activity_login&quot; /&gt;
     &lt;activity android:name=&quot;.MainActivity&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
     &lt;/activity&gt;
   &lt;/application&gt; 

Activity_main.xml

 &lt;androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
     android:layout_height=&quot;match_parent&quot;
     tools:context=&quot;.MainActivity&quot;&gt;

  &lt;TextView
    android:id=&quot;@+id/textView1&quot;
    android:layout_width=&quot;369dp&quot;
    android:layout_height=&quot;41dp&quot;
    android:layout_marginStart=&quot;5dp&quot;
    android:layout_marginLeft=&quot;42dp&quot;
    android:fontFamily=&quot;sans-serif-condensed-light&quot;
    android:text=&quot; Welcome To App&quot;
    android:textColor=&quot;#3888f1&quot;
    android:textStyle=&quot;bold&quot;
    android:textSize=&quot;30sp&quot;
    android:typeface=&quot;normal&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintHorizontal_bias=&quot;0.297&quot;
    app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
    app:layout_constraintRight_toRightOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:layout_constraintVertical_bias=&quot;0.023&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/textView2&quot;
    android:layout_width=&quot;369dp&quot;
    android:layout_height=&quot;41dp&quot;
    android:layout_marginStart=&quot;28dp&quot;
    android:layout_marginLeft=&quot;42dp&quot;
    android:layout_below=&quot;@+id/textView1&quot;
    android:fontFamily=&quot;sans-serif-condensed-light&quot;
    android:text=&quot;Oyyaah! Your account has been varified.
                  Please update your details and password&quot;
    android:textColor=&quot;#000&quot;
    android:textSize=&quot;18sp&quot;
    android:typeface=&quot;normal&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintHorizontal_bias=&quot;0.0&quot;
    app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
    app:layout_constraintRight_toRightOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:layout_constraintVertical_bias=&quot;0.111&quot; /&gt;

&lt;ImageView
    android:id=&quot;@+id/imageView&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    app:srcCompat=&quot;@drawable/welcome&quot;
    tools:ignore=&quot;MissingConstraints&quot;
    tools:layout_editor_absoluteX=&quot;0dp&quot;
    tools:layout_editor_absoluteY=&quot;0dp&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/textView3&quot;
    android:layout_width=&quot;369dp&quot;
    android:layout_height=&quot;41dp&quot;
    android:layout_marginStart=&quot;32dp&quot;
    android:layout_marginLeft=&quot;42dp&quot;
    android:fontFamily=&quot;sans-serif-condensed-light&quot;
    android:text=&quot;Enter Name&quot;
    android:textColor=&quot;#000&quot;
    android:textSize=&quot;18sp&quot;
    android:typeface=&quot;normal&quot;
    android:layout_below=&quot;@+id/imageView&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintHorizontal_bias=&quot;0.0&quot;
    app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
    app:layout_constraintRight_toRightOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:layout_constraintVertical_bias=&quot;0.813&quot; /&gt;

&lt;EditText
    android:id=&quot;@+id/editText5&quot;
    style=&quot;@style/Widget.AppCompat.EditText&quot;
    android:layout_width=&quot;260dp&quot;
    android:layout_height=&quot;41dp&quot;
    android:layout_above=&quot;@+id/Button1&quot;
    android:layout_below=&quot;@id/textView3&quot;
    android:layout_alignBottom=&quot;@id/textView3&quot;
    android:layout_alignParentBottom=&quot;true&quot;
    android:layout_gravity=&quot;center&quot;
    android:layout_marginTop=&quot;600dp&quot;
    android:layout_marginLeft=&quot;47dp&quot;
    app:layout_constraintVertical_bias=&quot;0.813&quot;
    android:ems=&quot;10&quot;
    android:background=&quot;@drawable/custom_input&quot;
    android:inputType=&quot;textPersonName|text&quot;
    android:singleLine=&quot;false&quot;
    android:hint=&quot;@string/type_your_name&quot;
    android:paddingLeft=&quot;15dp&quot;
    app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
    app:layout_constraintRight_toRightOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    tools:ignore=&quot;MissingConstraints&quot; /&gt;

&lt;Button
    android:id=&quot;@+id/Button1&quot;
    android:layout_width=&quot;200dp&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@+id/editText5&quot;
    android:layout_marginBottom=&quot;16dp&quot;
    android:background=&quot;#3888f1&quot;
    android:gravity=&quot;center|center_horizontal|center_vertical&quot;
    android:text=&quot;Get Started&quot;
    android:textColor=&quot;#fff&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintHorizontal_bias=&quot;0.497&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    tools:ignore=&quot;MissingConstraints&quot; /&gt;

</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:

&lt;activity android:name=&quot;.login&quot;&gt;&lt;/activity&gt;

and this:

&lt;activity
    android:name=&quot;.ui.login.LoginActivity&quot;
    android:label=&quot;@string/title_activity_login&quot; /&gt;

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)

huangapple
  • 本文由 发表于 2020年8月21日 18:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定