마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 스튜디오 intent 사용시 화면 강제종료되는 이유가 궁금합니다. [closed]

0 추천
<메인 엑티비티>

package kr.ac.dongyang.a07_activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public void onClickInterpark (View v) {
       // String str = getString(R.string.interpark_msg);
      //  Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();

        Intent intentM = new Intent(getApplicationContext(), RecentActivity.class);
        intentM.putExtra("web","interpark");
        startActivity(intentM);

    }

    public void   onClickimg (View v) {
      int nId = v.getId(); // 아이디를 읽어오는거

      switch (nId) {
          case R.id.imageViewYouTube :
              Toast.makeText(getApplicationContext(), R.string.youtube_msg, Toast.LENGTH_LONG).show();

              break;
          case R.id.imageViewFacebook :
              Toast.makeText(getApplicationContext(), R.string.facebook_msg, Toast.LENGTH_LONG).show();
              break;
          case R.id.imageViewNaver :
              //Toast.makeText(getApplicationContext(), R.string.naver_comic_msg, Toast.LENGTH_LONG).show();
          {
              Intent intentM = new Intent(this, RecentActivity.class);
              intentM.putExtra("web","naver_comic");
              startActivity(intentM);
          }
              break;
      }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textInterpark = findViewById(R.id.textViewInterpark);
        TextView textFacebook = findViewById(R.id.textViewFacebook);
        TextView textYoutube = findViewById(R.id.textViewYouTube);
        TextView textNaver = findViewById(R.id.textViewNaver);

        textInterpark.setOnClickListener(new View.OnClickListener() {
            public void onClick(View V) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.interpark.com"));
                startActivity(intent);
            }

        });
        textFacebook.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com"));
                startActivity(intent);
            }
        });


        View.OnClickListener oc1 = new View.OnClickListener() {
            public void onClick(View v) {
                if (v.getId() == R.id.textViewYouTube) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com"));
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.comic.naver.com"));
                    startActivity(intent);
                }
            }
        };
        textYoutube.setOnClickListener(oc1);
        textNaver.setOnClickListener(oc1 );



}
}

<리센트엑티비티>

<package kr.ac.dongyang.a07_activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class RecentActivity extends AppCompatActivity {
    private ImageView imgView1, imgView2, imgView3 ;
    private TextView textTitle, textView1, textView2, textView3;
    private String web;

    public void onClickHome(View v) {
        finish();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent);


        imgView1 = findViewById(R.id.imageView1);
        imgView2 = findViewById(R.id.imageView2);
        imgView3 = findViewById(R.id.imageView3);
        textView1 = findViewById(R.id.textView1);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);

        Intent intentM = getIntent();
        if(intentM != null) {
            web = intentM.getStringExtra("web");
            switch (web) {

                case "interpark" :
                    textTitle.setText(R.string.title_interpark);
                    imgView1.setImageResource(R.drawable.img1_1);
                    imgView2.setImageResource(R.drawable.img1_2);
                    imgView3.setImageResource(R.drawable.img1_3);
                    textView1.setText(R.string.img1_1);
                    textView2.setText(R.string.img1_2);
                    textView3.setText(R.string.img1_3);
                    break;

                case "naver_comic" :
                    textTitle.setText(R.string.title_naver);
                    imgView1.setImageResource(R.drawable.img4_1);
                    imgView2.setImageResource(R.drawable.img4_2);
                    imgView3.setImageResource(R.drawable.img4_3);
                    textView1.setText(R.string.img2_1);
                    textView2.setText(R.string.img2_2);
                    textView3.setText(R.string.img2_3);
                    break;
                default :
                    return;
            }
        }

        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentR = new Intent(getApplicationContext(),InfoActivity.class);
                if(web.equals("interpark")) {
                    intentR.putExtra("img", R.drawable.img1_1);
                    intentR.putExtra("text",getString(R.string.img1_1));
                } else {
                    intentR.putExtra("img", R.drawable.img4_1);
                    intentR.putExtra("text",getString(R.string.img2_1));
                    startActivity(intentR);

                }
            }
        });
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentR = new Intent(getApplicationContext(),InfoActivity.class);
                if(web.equals("interpark")) {
                    intentR.putExtra("img", R.drawable.img1_2);
                    intentR.putExtra("text",getString(R.string.img1_2));
                } else {
                    intentR.putExtra("img", R.drawable.img4_2);
                    intentR.putExtra("text",getString(R.string.img2_2));
                    startActivity(intentR);

                }
            }
        });

        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentR = new Intent(getApplicationContext(),InfoActivity.class);
                if(web.equals("interpark")) {
                    intentR.putExtra("img", R.drawable.img1_3);
                    intentR.putExtra("text",getString(R.string.img1_3));
                } else {
                    intentR.putExtra("img", R.drawable.img4_3);
                    intentR.putExtra("text",getString(R.string.img2_3));
                    startActivity(intentR);

                }
            }
        });
    }
}

메인엑티비티에서 리센트엑티비티로 넘어갈때 화면이 강제종료됩니다.

오류로그를 잘 보지는 못하지만 메니피스트에서 추가를 안해줘서 생기는 오류는 아닌 것 같고

null때문에 생기는 오류같은데 어디서 오류가 발생한건지 모르겠습니다..

 

질문을 종료한 이유: 제가 바보였네요.. 해결했습니다.  textTitle = findViewById(R.id.textViewTitle); 를 추가를 안했으니 값을 얻어오질못해 null이 나오던 거였습니다. 이제 대학교1학년인데 아직 갈 길이 멀지만 차근차근 해야겠네용
느허허헝 (120 포인트) 님이 2020년 11월 4일 질문
느허허헝님이 2020년 11월 4일 closed
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
        at kr.ac.dongyang.a07_activity.RecentActivity.onCreate(RecentActivity.java:38)
계속 찾아보니 38번째 줄에서 NullPoint에러가 발생한다는것은
 switch에서 쓴 web이 null이 발생하는건데 여기서 왜 null이 나오는지 잘모르겟습니다..
제가 바보였네요.. 해결했습니다.  textTitle = findViewById(R.id.textViewTitle); 를 추가를 안했으니 값을 얻어오질못해 null이 나오던 거였습니다.
이제 대학교1학년인데 아직 갈 길이 멀지만 차근차근 해야겠네용
2020-11-04 14:55:49.846 22634-22634/kr.ac.dongyang.a07_activity E/AndroidRuntime: FATAL EXCEPTION: main
    Process: kr.ac.dongyang.a07_activity, PID: 22634

    java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.ac.dongyang.a07_activity/kr.ac.dongyang.a07_activity.RecentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method
 'int java.lang.String.hashCode()' on a null object reference

        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at kr.ac.dongyang.a07_activity.RecentActivity.onCreate(RecentActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
...