2013-04-05 2 views
2

확인을 위해 devoloper.android.com 사이트의 "MyFirstApp"자습서를 살펴 보겠습니다. 나는 마지막 튜토리얼을 진행하고 있고, 나는 당신이 할 수있는 첫 번째 장을 위해 모든 것을했다. 유일한 것은 "다른 활동 시작"수업이 끝날 때 마지막에 앱을 실행하라는 의미입니다. 글쎄, 실행하면 에뮬레이터에서 클릭 가능한 버튼으로 표시되지만 클릭하면 런타임 오류가 발생하고 강제 종료됩니다. 나는 이것에 대한 연구를 해보려고했지만, 무슨 일이 일어나고 있는지에 대한 단서는 아닙니다. 오류 : java.lang.IlleglStateException : 클래스 com.example.MyFirstApp.MainActivity에서 클래스 sendmessage (view)를 찾을 수 없습니다. android.widet.B에있는보기 클래스의 onClick 처리기 나는 가고 있습니다.onClick 메서드 문제

MainActivity :

package com.example.myfirstapp; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 

public class MainActivity extends Activity { 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";  

    /** 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.edit_message); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
     // Do something in response to button 
    } 

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

} 

activity_display_message :

<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:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

DisplayMessageActivit이에 대한 코드를 표시합니다 y.java : 당신이 Button onClick()에 대한 XML의 함수 이름의 철자처럼

package com.example.myfirstapp; 

import android.annotation.SuppressLint; 
import android.annotation.TargetApi; 
import android.app.ActionBar; 
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); 

     // Get the message from the intent 
     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

     // 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); 

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

    private ActionBar getupActionBar() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    /** 
    * Set up the {@link android.app.ActionBar}, if the API is available. 
    */ 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    private void setupActionBar() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 
+0

xml에 'Button'이 있습니까? 나는 당신이 그것 모두를 게시했다고 생각하지 않습니다 – codeMagic

+0

내 대답을 시도해야합니다 – Harshid

답변

3

것 같습니다. 당신은 당신의 자바 코드에서 대문자 "m"이

public void sendMessage(View view) { 

아마

<Button 
... 
android:onClick="sendmessage"/> 

는, 자바 표준을 준수

에 XML로 변경하려면 당신의 XML의 소문자 "m"
android:onClick="sendMessage"/> 
+0

이게 당신의 문제를 해결 했습니까? – codeMagic

+0

대문자에 대해 언급 한 바를 수행했습니다. 나는 그 모든 것을 고쳤지만, 여전히 일어나고있다. 이것은 내가 본 것입니다 : http : //imageshack.us/photo/my-images/51/problem3y.png/ – Androidinventor

+0

나는 그 링크에서 아무것도 볼 수 없습니다. 오류 메시지가 나타나면 logcat – codeMagic

0

나는 의도로부터 잘못된 메시지를 받고 있다고 생각합니다. EXTRA_MESSAGE이 내용이고 message이 식별자입니다.

등 코드 DisplayMessageActivity.java

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

실제로해야한다 :

String message = intent.getStringExtra(message); 
0

당신은 다음과 다음을 onClick()으로이 방법을 버튼을 선언해야 당신의 activity_main.xml에서 볼 수 일하게 될거야.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click" 
     android:onClick="sendMessage()"/> 

</LinearLayout> 
+1

OP는 내가 제안했을 때 OP를 입력하기 6 시간 전에 시도 했으므로 문제가 해결되었습니다. 하지만 xml의 함수 이름에는 "()"을 사용하지 않습니다. – codeMagic