【问题标题】:How to go second activity from first activity on button click and Third activity from Second on button click in android如何在按钮单击时从第一个活动中进行第二个活动,在按钮单击时从第二个活动中进行第三个活动
【发布时间】:2016-07-27 21:12:52
【问题描述】:

大家好,我在我的 android 应用程序中使用了 3 个活动,它们是

1.main_activity

2.第二次活动

3.第三个活动

当我尝试通过单击主要活动的按钮从主要活动中打开 第二个活动 时,我遇到了问题。应用程序崩溃并且无法运行

我还需要通过一个按钮打开我的第三个​​活动,点击第二个活动当它打开时

由于我是 android 编码的新手,我无法理解我在编码中做错了什么。我想寻求有关信息,也需要任何可以帮助我解决此问题的人

提前致谢

下面是完整的代码,得到的错误是这个

07-27 12:13:51.887 23039-23039/com.example.mohammadzakriya.tech_bgkapp E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.example.mohammadzakriya.tech_bgkapp, PID: 23039
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammadzakriya.tech_bgkapp/com.example.mohammadzakriya.tech_bgkapp.Secondactivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
         at android.app.ActivityThread.-wrap11(ActivityThread.java)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:148)
         at android.app.ActivityThread.main(ActivityThread.java:5417)
         at java.lang.reflect.Method.invoke(Native Method)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
         at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.OnclickButtonListener(Secondactivity.java:24)
         at com.example.mohammadzakriya.tech_bgkapp.Secondactivity.onCreate(Secondactivity.java:19)
         at android.app.Activity.performCreate(Activity.java:6237)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:5417) 
         at java.lang.reflect.Method.invoke(Native Method) "**
                                                                                             at java.lang.reflect.Method.invoke(Native Method) 

这是 Android 主 Activity.java 文件:

package com.example.mohammadzakriya.tech_bgkapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity{

    public static Button button_smb;


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

    public void OnclickButtonListener(){
        button_smb =(Button)findViewById(R.id.button);
        button_smb.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent("com.example.mohammadzakriya.tech_bgkapp.Secondactivity");
                        startActivity(intent);
                    }
                }
        );
    }
}

这是第二个 Activity.java 文件:

package com.example.mohammadzakriya.tech_bgkapp;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Secondactivity extends Activity{

    public static Button button_smb1;


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

    public void OnclickButtonListener(){
        button_smb1 =(Button)findViewById(R.id.button);
        button_smb1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent myintent = new Intent("com.example.mohammadzakriya.tech_bgkapp.ThirdActivity");
                        startActivity(myintent);
                    }
                }
        );
    }
}

主活动 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mohammadzakriya.tech_bgkapp.MainActivity"
    android:background="@drawable/frontpic">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to proceed"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold|italic|normal"
        android:textSize="19dp"
        android:background="#d55b26f8"
        android:textColor="#e2e0e0" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="     WELCOME TO BAGALKOT APP"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textStyle="bold|italic"
        android:layout_marginTop="50dp"
        android:textSize="25dp"
        android:background="#fbe47d0e"
        android:textColor="#e2e0e0" />
</RelativeLayout>
        

第二个活动 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mohammadzakriya.tech_bgkapp.Secondactivity"
    android:background="#eb5d16ea">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ELECTRONICS"
        android:id="@+id/button2"
        android:layout_marginTop="42dp"
        android:textStyle="bold|italic|normal"
        android:background="#eec16d0d"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:textSize="18dp"
        android:textColor="#e2e0e0"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="              SELECT THE CATEGORY"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/button2"
        android:layout_marginTop="15dp"
        android:textStyle="bold|italic|"
        android:background="#fbe47d0e"
        android:textColor="#e2e0e0"/>
</RelativeLayout>
        

Android 清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mohammadzakriya.tech_bgkapp">

    <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"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Secondactivity"
            android:label="Tech-Bgk App">
            <intent-filter>
                <action android:name="com.example.mohammadzakriya.tech_bgkapp.Secondactivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".ThirdActivity"
                  android:label="Tech-Bgk App">
            <intent-filter>
                <action android:name="com.example.mohammadzakriya.tech_bgkapp.ThirdActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
    </application>

</manifest>

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    请尝试以下代码

    按钮点击MainActivity进入SecondActivity

       @Override
       public void onClick(View view) {
           Intent myintent = new Intent(MainActivity.this, SecondActivity.class);
           startActivity(myintent);
       }
    

    然后Button点击SecondActivity进入ThirdActivity

       @Override
       public void onClick(View view) {
           Intent myintent = new Intent(SecondActivity.this, ThirdActivity.class);
           startActivity(myintent);
       }
    

    已编辑

    除了在 Secondactivity 中初始化 button_smb1 之外,您的代码中的一切都很好。

    请将 Secondactivity 中的更改为

    button_smb1 =(Button)findViewById(R.id.button2);

    现在它可以工作了。无需更改任何其他内容。

    【讨论】:

    • 完成了你上面提到的编辑但无法解决错误。在应用编辑之前我需要在 Android 清单文件中进行更改吗?
    • 请在第二个活动button_smb1 =(Button)findViewById(R.id.button2); 中执行此操作。现在它将起作用。无需更改任何内容。
    【解决方案2】:

    试试看,

    Intent intent = new Intent(MainActivity.this, Secondactivity.class);
    startActivity(intent);
    

    这应该对你有帮助

    【讨论】:

      【解决方案3】:

      更改以下代码
      主要活动到第二个活动

      Intent intent = new Intent(this, Secondactivity.class);
      startActivity(intent);
      

      第二个活动到第三个活动

      Intent intent = new Intent(this, ThirdActivity.class);
      startActivity(intent);
      

      【讨论】:

        【解决方案4】:

        在您的 mainActivity 文件中替换以下代码。

        button_smb.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(MainActivity.this,Secondactivity.class);
                                startActivity(intent);
                            }
                        }
                );
        

        【讨论】:

        • 这里this 将引用OnClickListener 对象。所以我认为它不会起作用。
        【解决方案5】:

        使用这个:

        Intent intent = new Intent(MainActivity.this, Secondactivity.class);
        startActivity(intent);
        

        根据我的经验,这就是您通过代码从一个 Activity 移动到另一个 Activity 的方式。

        还有一件事,添加onClickListener() 是一种奇怪的方式,如果它只是一个简单的“第一个活动到第二个活动”,你应该直接声明按钮并设置onClickListener()

        Button button_smb =(Button)findViewById(R.id.button);
        button_smb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(this, Secondactivity.class);
                startActivity(intent);
                }
        }
        

        【讨论】:

        • 我认为这里 this 是在引用 OnClickListener 对象,而不是引用 Activity。所以我认为它会给你一个错误或者它不会给你想要的输出。
        • 哦,我几乎总是忘记,抱歉,正在编辑它
        • 这就是为什么总是尝试使用 Context 对象,它会保证你的安全。
        • 上下文对象?那是什么?
        • Context context = MainActivity.this;onCreate 方法中初始化它。更安全。
        【解决方案6】:

        为了避免新的,我更喜欢使用这种方法:

        public class MyActivity extends AppCompactActivity implents View.OnClickListener{
        
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ....
            button.setOnClickListener(this);
            ...
        }
        
        @Override
        public void onClick(View view) {
        
            Intent i;
            switch (view.getId()) {
                case R.id.button1:                     
                    i = new Intent(this, SecondActivity.class);
                    i.putExtra("extra", 0); //if you want pass some info to other activity
                    startActivity(i);
                    break;
                 default:
                    break;
             }
        }
        

        这样你可以在一个函数中处理更多的onclick,通过组件的id切换

        【讨论】:

          猜你喜欢
          • 2015-05-26
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多