-4

Android 기기 2.3 테스트 기기 (Tecno-Tecno-6.0 Marshallow)를 사용 중입니다.Android : 활동 간 이동

나는 안드로이드 애플 리케이션 개발에 처음이다. 나는 3 개의 Activity (MainActivity, DisplayMessageActivity 및 ReadMessageActivity) .screens를 가지고있다.

하위 활동 인 DisplayMessageActivity에 대한 parentActivity 및 DisplayMessageActivity에서 ReadMessageActivity로 MainActivity를 앞뒤로 탐색 할 수 있습니다.

그러나 ReadMessageActivity에서 DisplayMessageActivity로 되돌아 갈 수는 없습니다. 이렇게하면 응용 프로그램이 "오류로 인해 충돌합니다. 안타깝게도 AppName이을 중지했습니다."

이 응용 프로그램은 정상적으로 작동하지만 ... 제 2의 활동으로 돌아갈 때 제 3의 활동이 응용 프로그램을 중단한다는 점을 제외하고는이 응용 프로그램이 정상적으로 작동합니다.

아래 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; 
} 
    } 
} 
+0

여기 예외와 함께 stacktrace를 넣어 –

+0

나는 안드로이드 애플 리케이션 dev에 처음이에요 ... 어떻게 Studio에서 stackTrace를 얻을 수 있습니까? – user3134352

+0

@ 루이즈, 나는 – user3134352

답변

0

업데이트하여 다음과 같이 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 신청서. 희망이 작동합니다 ~

+0

나는 당신의 열정을 좋아하지만 ... 당신의 대답은 효과가 없습니다 .. 오히려, 그것의 위치에서 '위로'버튼을 제거합니다. 돌아가려면 기기의 취소 버튼을 눌러야합니다. 제발 더 많은 도움이 필요합니다. – user3134352

+0

@ 루이즈, 나는 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이 : android.app.ActivityThread.performLaunchActivity에서 널 객체 참조 가상 메소드 'java.lang.String의 android.os.Bundle.getString (java.lang.String의)' – user3134352

+0

를 호출 시도 (android.app.ActivityThread $ H.handleMessage에서 android.app.ActivityThread.handleLaunchActivity android.app.ActivityThread.-wrap11에서 (ActivityThread.java:2654) (ActivityThread.java) (ActivityThread.java에서 ActivityThread.java:2572) : 1488) android.os.Handler.dispatchMessage (Handler.java:111) android.os.Looper.loop (Looper.java:207) at android.app.ActivityThread.main (ActivityThread.java:5763) in java. com.android.internal.os.ZygoteInit.main에서 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:789) (ZygoteInit.java에서 lang.reflect.Method.invoke (기본 방법) – user3134352