【问题标题】:Android: Navigating Between ActivitiesAndroid:活动之间的导航
【发布时间】:2017-04-11 17:02:44
【问题描述】:

我使用的是 Android Studio 2.3 测试设备(Tecno-Tecno-6.0 Marshallow)。

我是 Android 应用程序开发的新手.. 我有 3 个 Activity(MainActivity、DisplayMessageActivity 和 ReadMessageActivity).screens。

我可以从作为父活动的 MainActivity 到作为子活动的 DisplayMessageActivity 以及从 DisplayMessageActivity 到 ReadMessageActivity 来回导航。

但我无法从 ReadMessageActivity 导航回 DisplayMessageActivity。 当我这样做时,应用程序崩溃并出现错误“不幸的是,AppName 已停止。”

这个应用程序运行良好...除了第 3 个活动在返回到第 2 个活动时使应用程序崩溃。

请查看下面的 AndroidManifest.xml 和不同的 Activity.xml 代码,帮助我应对这个挑战。 提前谢谢你。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

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

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

    <activity
        android:name=".DisplayMessageActivity"
        android:parentActivityName=".MainActivity"
        android:label="@string/welcome_screen_title"
        android:finishOnTaskLaunch="true"
        android:launchMode="standard">

        <!-- The meta-data tag is required if you support API level 15 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>


    <activity android:name=".ReadMessageActivity"
        android:parentActivityName=".DisplayMessageActivity"
        android:label="@string/question_screen"
        android:allowTaskReparenting="true">

        <!-- The meta-data tag is required if you support API level 15 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".DisplayMessageActivity" />
    </activity>
</application>

MainActivity.xml

package com.example.examinationportal;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   String message = "";

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

    /** Called when the user taps the Send button */
    public void loginUser(View login) {
        Intent intent = new Intent(this, DisplayWelcomeScreen.class);//start the ui
        //initialize the status bar textview control
        TextView statusBar = (TextView) findViewById(R.id.textViewStatusMsg);
        //initialize textViewLogin
        TextView titleLogin = (TextView) findViewById(R.id.textViewLogin) ;
        titleLogin.setTextColor(0x01060013);//colors

        EditText un = (EditText) findViewById(R.id.editTextUsername);//username field
        EditText pw = (EditText) findViewById(R.id.editTextPassword);//password field

        String username = un.getText().toString();//convert username and password to string and parse
        String password = pw.getText().toString();//them to variables

        //check the login
       if(username.equals("admin")  && password.equals("admin")){//compare the username and password entered by user with the defaul
           message = "Welcome"; //message for successful login
           String msg = "Login Successful!";//this message will be displayed on the status bar
           statusBar.setTextColor(Color.parseColor("#ff0000"));
           statusBar.setBackgroundColor(Color.parseColor("#d3d3d3"));
           statusBar.setText(msg);//disp the msg for unsuccessful login

           Bundle bundle = new Bundle();//bundle the message and parse it to the next activity
           bundle.putString("dispMsg", message);//bundle the message using the variable dispMsg
           intent.putExtras(bundle);
           startActivity(intent);
       }else{
           message = "Invalid Login! You are not authorised to view this page...";

           //statusBar.setText(null);//status bar textview
           //statusBar.setTextColor(getResources().getColor(R.color.colorAccent));
           //statusBar.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
           statusBar.setTextColor(Color.parseColor("#ff0000"));
           statusBar.setBackgroundColor(Color.parseColor("#d3d3d3"));
           statusBar.setText(message);//disp the msg for unsuccessful login
        }

    }
}

DisplayMessageActivity.xml

package com.example.examinationportal;

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

public class DisplayWelcomeScreen extends AppCompatActivity {

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


    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    Bundle bundle = getIntent().getExtras();
    String showMsg = bundle.getString("dispMsg");

    // Capture the layout's TextView and set the string as its text
    TextView textView = (TextView) findViewById(R.id.textViewWelcome);
    textView.setText(showMsg);

        //disable some controls when user is not logged in
        View buttonCSS342 = findViewById(R.id.buttonCSS342);



        View buttonCSS352 = findViewById(R.id.buttonCSS352);
        View buttonCSS354 = findViewById(R.id.buttonCSS354);
        View buttonCSS356 = findViewById(R.id.buttonCSS356);
        View buttonCSS381 = findViewById(R.id.buttonCSS381);
        View buttonPCR362 = findViewById(R.id.buttonPCR362);
  }
public void courseClicked(View v) {

        String course = "";
        Intent intent = new Intent(this, CSS_342_Questions.class);
        int qNum = 0;

       switch (v.getId()){
           case R.id.buttonCSS342:
               course = "CSS 342";
               qNum=1;
               break;
           case R.id.buttonCSS352:
               course = "CSS 352";
               qNum=1;
               break;
           case R.id.buttonCSS354:
               course = "CSS 354";
               qNum=1;
               break;
           case R.id.buttonCSS356:
               course = "CSS 356";
               qNum=1;
               break;
           case R.id.buttonCSS381:
               course = "CSS 381";
               qNum=1;
               break;
           case R.id.buttonPCR362:
               course = "PCR 362";
               qNum=1;
               break;
       }
        Bundle bundle = new Bundle();
        bundle.putString("dispCode", course);
        bundle.putInt("qNum", qNum);
        intent.putExtras(bundle);

        startActivity(intent);

    }
}

ReadMessageActivity.xml

package com.example.examinationportal;


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

import org.w3c.dom.Text;

public class CSS_342_Questions extends AppCompatActivity {
    public int qNum = 0;
    public String showCode ="";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_css_342__questions);

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        Bundle bundle = getIntent().getExtras();
        showCode = bundle.getString("dispCode");
        qNum = bundle.getInt("qNum");

        TextView textView = (TextView) findViewById(R.id.textViewCourseTitle);
        TextView qn = (TextView)findViewById(R.id.textViewQn);
        TextView q = (TextView)findViewById(R.id.textViewQ);

        WebView ans = (WebView) findViewById(R.id.textViewAns);
        ans.getSettings().setLoadsImagesAutomatically(true);
        ans.getSettings().setJavaScriptEnabled(true);
        ans.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        ans.getSettings().setBuiltInZoomControls(true);

View buttonPrev = findViewById(R.id.buttonPrevious);
        View buttonNext = findViewById(R.id.buttonNext);

        if(showCode.equals("CSS 342") && qNum == 1) {
            String showTitle = "Safety Management and Loss Prevention";
            textView.setText(showTitle);// Capture the layout's TextView and set the string as its text
            qn.setText(Questions.css342_q1[0]);
            q.setText(Questions.css342_q1[1]);
            ans.loadUrl("file:///android_asset/css342_q1.html");
            buttonPrev.setVisibility( View.INVISIBLE);
            qNum = 1;
        }else if(showCode.equals("CSS 352") && qNum == 1){
            String showTitle = "Crime and Crime Theories";
            // Capture the layout's TextView and set the string as its text
            textView.setText(showTitle);
            qn.setText(Questions.css352_q1[0]);
            q.setText(Questions.css352_q1[1]);
            ans.loadUrl("file:///android_asset/css352_q1.html");
            buttonPrev.setVisibility(View.INVISIBLE);
            qNum = 1;
 }
    }
}

【问题讨论】:

  • 把你的堆栈跟踪放在这里
  • 我是 android app dev 的新手...请问如何从 Studio 获取 stackTrace?
  • @Luiz,我想我找到了
  • @Luiz Fernando,我找到了应用程序的堆栈跟踪。请参阅 Ferdous Ahmed 回答下方评论中的详细信息。谢谢。

标签: android android-activity back-button android-navigation android-homebutton


【解决方案1】:

更新您的AndroidManifest.xml 如下:

.................
.........................

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

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

    <activity
        android:name=".DisplayWelcomeScreen"
        android:label="@string/welcome_screen_title">

    </activity>

    <activity android:name=".CSS_342_Questions"
        android:label="@string/question_screen">

    </activity>
</application>

...............
.......................

CleanRun 您的应用程序。希望能成功~

【讨论】:

  • 我喜欢你的热情......但你的回答不起作用......相反,它从其位置删除了“向上”按钮。我必须按设备上的取消按钮才能返回。我需要更多帮助。
  • @Luiz,我想我找到了 LogCat 堆栈跟踪......下面是它...... ---------崩溃开始 04-13 17:33:16.038 579 -579/com.example.examinationportal E/AndroidRuntime:致命异常:主进程:com.example.examinationportal,PID:579 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.examinationportal/com.example.examinationportal .DisplayWelcomeScreen}: java.lang.NullPointerException: 尝试调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'
  • 关于 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 在 android.app.ActivityThread 的空对象引用。 -wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 在 android.os.Handler.dispatchMessage(Handler.java:111) 在 android.os.Looper.loop(Looper. java:207) 在 android.app.ActivityThread.main(ActivityThread.java:5763) 在 java.lang.reflect.Method.invoke(Native Method)
  • at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) : java.lang.NullPointerException: 尝试在 com.example.examinationportal.DisplayWelcomeScreen.onCreate(DisplayWelcomeScreen) 的空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String)” .java:21) 在 android.app.Activity.performCreate(Activity.java:6375) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 在
  • android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 在 android.app.ActivityThread.-wrap11(ActivityThread.java ) 在 android.os.Looper.loop(Looper.java:207) 在 android.os.Handler.dispatchMessage(Handler.java:111) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 在 android .app.ActivityThread.main(ActivityThread.java:5763) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
相关资源
最近更新 更多