2013-11-03 6 views
0

나는 안드로이드 개발을 처음 접했고 작은 응용 프로그램을 만들기 위해 this tutorial을 팔로우하고 있습니다. 방금 약간의 코드를 변경했습니다. 보내기 버튼을 클릭하면 "불행히도이 첫 번째 앱이 중지되었습니다."라는 메시지가 나타납니다. 문제가 무엇이든간에 누군가 나에게 말해 줄 수 있습니까? 내 간단한 응용 프로그램이 멈춘 이유

는 I (여기서는 두 번째 클래스의 코드 블록 주석으로 도시 됨) 가이드에 주어진 코드를 대체하고, 여기에서 코드로 대체 코멘트 * 을 함유 띄는했다.

FIRST 활동 : - FIRST 활동의

package com.practice.firstapp1; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import com.practice.firstapp1.R; 

public class MainActivity extends Activity { 

    public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE"; 

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

    @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; 
    } 

    /** Called when the user clicks the Send button */ 
    /** Called when the user clicks the Send button */ 
    public void sendMessage(View view) { 
     Intent intent = new Intent(this, DisplayMessageActivity.class); 
     EditText editText = (EditText) findViewById(R.id.id1); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 

} 

LAYOUT : - 2ND 활동의

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:orientation="horizontal"> 
    <EditText android:id="@+id/id1" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/edit_message" /> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage" /> 
</LinearLayout> 

CODE : - 2ND 활동의

package com.practice.firstapp1; 

    import android.annotation.SuppressLint; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Build; 
    import android.os.Bundle; 
    import android.support.v4.app.NavUtils; 
    import android.view.MenuItem; 
    import android.widget.TextView; 

    public class DisplayMessageActivity extends Activity { 

     @SuppressLint("NewApi") 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_display_message); 

      // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       // Show the Up button in the action bar. 
       getActionBar().setDisplayHomeAsUpEnabled(true); 
      } 

      //Get the intent which invoked this activity and read its extra's data into the string named message. 
      Intent intent = getIntent(); 
      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

      TextView textView = (TextView) findViewById(R.id.text_view_2); //************ 
      textView.setText(message);//*********** 

      setContentView(textView);//*********** 
//The above three statements along with statements from its layout (which have been given *s in comments have been placed instead of the statements in the following block comment. Because I wanted to create the TextView in layout.xml and handle its work in code. 

/* 
    // Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    // Set the text view as the activity layout 
    setContentView(textView); 
*/ 

     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()) { 
      case android.R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 
    } 

LAYOUT : -

<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".DisplayMessageActivity" > 

    <TextView <!--//*********************************--> 
     android:id="@+id/text_view_2" <!--//***********************--> 
     android:layout_width="wrap_content" <!--//**********************--> 
     android:layout_height="wrap_content" <!--//**************--> 
     android:textSize="40sp" /> <!--//**************************--> 


</RelativeLayout> 

안드로이드 매니페스트 파일 : -

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.practice.firstapp1" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.practice.firstapp1.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.practice.firstapp1.DisplayMessageActivity" 
      android:label="@string/title_activity_display_message" 
      android:parentActivityName="com.practice.firstapp1.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.practice.firstapp1.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 
+1

에 대한 필요가 당신은 로그 캣에서 오류를하시기 바랍니다 게시 할 수 없었다? –

+0

매니페스트 파일에서 활동을 선언하셨습니까? 게시물 매니페스트 파일 – Raghunandan

+0

및이'setContentView (textView); 필요하지 않습니다 – Raghunandan

답변

1

의견에서이

I havn't done anything in the manifest file. I think it is done automatically when we are using eclipse. 

예 일식은 새로운 안드로이드 활동을 만들 경우 명시하는 활동을 추가 않습니다.

매니페스트가 정상적으로 보입니다. 아래 라인

편집 1 제거

setContentView(textView); 

activity_display_message.xml 것은 이미 텍스트 뷰를 가지고 있으며, 여러분의 코드에서 당신은 당신의 텍스트 뷰 설정하고 텍스트를 초기화합니다. TextView에 이미 상위 항목이 있습니다.

setContentView(textView)

+0

매니페스트에 이미 두 가지 활동이 모두 포함되어 있습니다. – user2882662

+0

@ user2882662 okcat logcat을 게시하십시오. Eclipse에서 goto windows -> open perspective -> open other -> ddms를 클릭하십시오 logcat을 클릭하십시오. 여기에 예외 게시물이 나타나야합니다. 확인을 위해 매니페스트 파일을 게시하는 것이 아무런 해가되지 않습니다. – Raghunandan

+0

아래 두 가지 대답을 참조하십시오. 로그 고양이와 명단. 나는 코멘트에 이미지를 추가하는 방법을 몰랐다. – user2882662

관련 문제