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

안드로이드 intent이용한 화면전환 질문입니다

0 추천
public class MainActivity extends Activity {

	EditText strEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);
        strEdit = (EditText)findViewById(R.id.editText1);
        Button launch=(Button)findViewById(R.id.button_1);
        launch.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v){
        		Intent intent = new Intent(MainActivity.this,Activity2.class);
        		startActivity(intent);
        	}
        });
    }
    
}

Main Activity입니다.

public class Activity2 extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("MyTag", "Print Test Log3 : "+savedInstanceState);
		super.onCreate(savedInstanceState);
		Log.d("MyTag", "Print Test Log4"+R.layout.activity_activity2);
		setContentView(R.layout.activity_activity2);
		Log.d("MyTag", "Print Test Log5");
//		Intent intent = getIntent();
		Log.d("MyTag", "Print Test Log6");
//		String str = intent.getExtras().getString("str").toString();
//		TextView tv = (TextView)findViewById(R.id.tv_2);
//		tv.setText(str);
	}
}

로그를 찍어 보면

setContentView(R.layout.activity_activity2); 이 줄에서 에러가 나는데, 이유를 알 수가 없습니다. xml 레이아웃이 잘못된 건 아닐까 생각했는데, 다른 작동하는 xml파일로 바꾸어도 똑같은 에러가 납니다.

익명사용자 님이 2013년 5월 3일 질문

2개의 답변

0 추천
 
채택된 답변
xml을 올려 봐 주세요. 제가 보기에는 xml에서 문제가 있는 것 같네요.

아니면 매니페스트에서 activity2가 등록이 되어있는지 확인 해 보세요.

매니페스트에서 인텐트가 추가 되지 않으면 인텐트가 넘어가지 않습니다.
센스가이 (2,010 포인트) 님이 2013년 5월 3일 답변
센스가이님이 2013년 5월 3일 수정
<LinearLayout 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=".Activity2"
    android:orientation="vertical"
    android:weightSum="3"
    >
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        
        android:layout_weight="1"
        android:weightSum="4"
        android:text="@string/hello_world" >
        <View
          
            android:layout_height="fill_parent"
            android:background="#FF0000FF"
            android:layout_weight="1"/>
        <View
           
            android:layout_height="fill_parent"
            android:background="#FF00FF00"
            android:layout_weight="2"/>
        <View
           
            android:layout_height="fill_parent"
            android:background="#FFFF0000"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:weightSum="2"
        android:text="@string/hello_world" >
        <View
           
            android:layout_height="fill_parent"
            android:background="#FF00FFFF"
            android:layout_weight="1"/>
        <View
           
            android:layout_height="fill_parent"
            android:background="#FFFFFF00"
            android:layout_weight="1"/>
    </LinearLayout>
    <TextView
        android:id = "@+id/tv_2"
        android:background="#FFFF00FF"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        
    />
   
    

</LinearLayout>
xml입니다.. 살펴봐주셔서 감사합니다..
0 추천
<LinearLayout 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=".Activity2" 
    android:orientation="vertical"
     android:weightSum="3">
     
    <LinearLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="1"
         android:weightSum="4"
         android:text="@string/hello_world" >
         <View 
           android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#FF0000FF"
             android:layout_weight="1"/>
         <View 
           android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#FF00FF00"
             android:layout_weight="2"/>
         <View 
           android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#FFFF0000"
             android:layout_weight="1"/>
     </LinearLayout>
     <LinearLayout
         android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="1"
         android:weightSum="2"
         android:text="@string/hello_world" >
         <View 
           android:layout_width="fill_parent"  
             android:layout_height="fill_parent"
             android:background="#FF00FFFF"
             android:layout_weight="1"/>
         <View 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:background="#FFFFFF00"
             android:layout_weight="1"/>
     </LinearLayout>
     <TextView
         android:id = "@+id/tv_2"
         android:background="#FFFF00FF"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="1"
     />
</LinearLayout>

위와 같이 해보시기 바랍니다 되는거 확인 했습니다. layout_width와 layout_height가 빠져있네요. 이부분을 wrap_contant나 match_parent, fill_parent로 수정해보시기 바랍니다.

센스가이 (2,010 포인트) 님이 2013년 5월 3일 답변
센스가이님이 2013년 5월 3일 수정
...