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

int형을 사용하려고하면 에러가 납니다. int어떻게 쓰나요?

0 추천

intent로 받아올때 

edittext 를 이용하기때문에

str으로 받아온 intent를 저장하고

 

이 str을 다시 int형으로 바꾸려고하면 에러가납니다.

 

왜그럴까요..

// 주석 처리한부분을 살리면   컴파일후에 휴대폰에서 실행시 액티비티전환에서 에러가 납니다.

//주석처리를 하면 액티비티 전환이 됩니다.

 

뭣때문일까요?

 

int a=str1 로 저장한후에

배경에서 rgb를 (255,a,b,c) 이런식으로 

첫번째 액티비티에서 abc를 받은후에 

두번째 액티비티에서는 받은 값의 색상을 나타나게 하려는데 왜이렇게 어렵죠.

 

a만 먼저 시범적으로 해버려는데 에러 원인을 모르겠습니다.

 

또한 

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
          getWindow().getDecorView().setBackgroundColor(Color.argb(255,0,255,255));
 
         
          setContentView(R.layout.main2);
 
이 부분을 가장 아래쪽에 놓았을때 역시 문제가 생기는데 왜그런가요?
package com.example.act_test;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity2 extends Activity {
//	int x;  
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 		 
          
        Intent intent = getIntent();
        
        String str1;  
        
          
        str1 = intent.getExtras().getString("edittext1");
       // int a;
    // int  x = Integer.valueOf(str1);
        TextView Textview1 = (TextView)findViewById(R.id.Textview1);
        
        Textview1.setText(str1+" " );
       
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		  
        getWindow().getDecorView().setBackgroundColor(Color.argb(255,0,255,255));

       
        setContentView(R.layout.main2);
        
              
       
      }

 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
 

 

밑에 소스대로 실행하면

package com.example.act_test;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity2 extends Activity {
//	int x;  
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 		 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		  
        getWindow().getDecorView().setBackgroundColor(Color.argb(255,0,255,255));

       
        setContentView(R.layout.main2);
        
              
         
        Intent intent = getIntent();
        
        String str1;  
        
          
        str1 = intent.getExtras().getString("edittext1");
       // int a;
    // int  x = Integer.valueOf(str1);
        TextView Textview1 = (TextView)findViewById(R.id.Textview1);
        
        Textview1.setText(str1+" " );
       
    
      }

 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
 

두번째 액티비티 실행시

 

화면은 청록색깔 배경에 edittext문구가 나옵니다.

 

int형 활용을 어떻게해야될지 도와주세요.

 

 

 

===================================

소스 원본 

mainactiviti 2

package com.example.act_test;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity2 extends Activity {
 //	int x;  
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 		 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		  
        getWindow().getDecorView().setBackgroundColor(Color.argb(255,0,255,255));

       
        setContentView(R.layout.main2);
        
              
         
        Intent intent = getIntent();
        
       String str1;  
        
          
       str1 = intent.getExtras().getString("edittext1");
       // int a;
    // int  x = Integer.valueOf(str1);
      // x=Integer.parseInt(str1);
       TextView Textview1 = (TextView)findViewById(R.id.Textview1);
        
       Textview1.setText(str1+" " );
       
    
      }

 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
 

 

 

익명사용자 님이 2013년 11월 14일 질문
2013년 11월 14일 수정

1개의 답변

0 추천
 
채택된 답변
aucd29 (218,390 포인트) 님이 2013년 11월 14일 답변
integer.parselnt(str) 이것도 이용해봤는데
안되네요.

뭐가 문제일까요?
str 이 null 인지 확인하세요
그리고 exception log 도 올려보세요
11-14 16:25:42.746: E/Trace(27820): error opening trace file: No such file or directory (2)
11-14 16:25:42.746: D/ActivityThread(27820): setTargetHeapUtilization:0.25
11-14 16:25:42.746: D/ActivityThread(27820): setTargetHeapIdealFree:8388608
11-14 16:25:42.746: D/ActivityThread(27820): setTargetHeapConcurrentStart:2097152
11-14 16:25:42.756: V/ActivityThread(27820): Class path: /data/app/com.example.act_test-2.apk, JNI path: /data/data/com.example.act_test/lib
11-14 16:25:42.836: I/Adreno200-EGL(27820): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build:  (CL3657481)
11-14 16:25:42.836: I/Adreno200-EGL(27820): Build Date: 04/24/13 Wed
11-14 16:25:42.836: I/Adreno200-EGL(27820): Local Branch: GK_LGE_JB_2.2_RB2_AU130
11-14 16:25:42.836: I/Adreno200-EGL(27820): Remote Branch:
11-14 16:25:42.836: I/Adreno200-EGL(27820): Local Patches:
11-14 16:25:42.836: I/Adreno200-EGL(27820): Reconstruct Branch:
11-14 16:25:42.856: I/Adreno200-C2D(27820): <c2d_wrapper_init:132>: C2D2 initializing a3xx version: libc2d2_a3xx.so
11-14 16:25:42.856: I/Adreno200-C2D(27820): <c2d_wrapper_init:196>: C2D2 wrapper initialized successfully
11-14 16:25:42.856: I/Adreno200-C2D(27820): <c2d_cmd_res_init:104>: Allocate CMD Buffer Address  0x5d66b000 0x1009a000
11-14 16:25:43.837: I/ViewRootImpl(27820): ViewRoot's MotionEvent : Down
11-14 16:25:43.947: I/ViewRootImpl(27820): ViewRoot's MotionEvent : Up
11-14 16:25:43.957: D/AudioManager(27820): playSoundEffect() : effectType = 0Request Package = com.example.act_test
11-14 16:25:43.997: W/dalvikvm(27820): threadid=1: thread exiting with uncaught exception (group=0x413aa438)
11-14 16:25:43.997: E/AndroidRuntime(27820): FATAL EXCEPTION: main
11-14 16:25:43.997: E/AndroidRuntime(27820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.act_test/com.example.act_test.MainActivity2}: java.lang.NumberFormatException: Invalid int: "EditText"
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.os.Looper.loop(Looper.java:137)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread.main(ActivityThread.java:4886)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at dalvik.system.NativeStart.main(Native Method)
11-14 16:25:43.997: E/AndroidRuntime(27820): Caused by: java.lang.NumberFormatException: Invalid int: "EditText"
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.Integer.invalidInt(Integer.java:138)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.Integer.parse(Integer.java:375)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.Integer.parseInt(Integer.java:366)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at java.lang.Integer.parseInt(Integer.java:332)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at com.example.act_test.MainActivity2.onCreate(MainActivity2.java:36)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.Activity.performCreate(Activity.java:5240)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
11-14 16:25:43.997: E/AndroidRuntime(27820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
11-14 16:25:43.997: E/AndroidRuntime(27820):     ... 11 more


str값은 edittext라는 값이들어가 있습니다.
Intent 로 에초에 보낼때 Integer.parseInt(str) 로 해서 보내면 어떨까요 ??

putExtras("editText",Integer.parseInt(str)); 이런식으로요
상관없습니다. 근데 보내는 부분 소스를 어케 하시나요?

1. putExtras("editText1", editText1.getText().toString()); 해서 보내시나요?

2. 받는 부분에 이름을 동일하게 적용하셨나요?
String str = intent.getExtras().getString("editText1");

3. 설마 값 자체가 EditText 인건 아니죠?
일단 오류 로그에는 java.lang.NumberFormatException: Invalid int: "EditText" 값이 그거라서 문제가 나타나는 것인데 말이죠 숫자를 넣어야 합니다.
아.....네
edittext에 값이 숫자가 아니니깐 에러가 생기네요.
감사합니다.
...