2016-02-27 4 views
0

Когда я помещаю эмулятор, а после приложения у меня есть EditText и Button. Но, когда я нажимаю на кнопку, приложение падает. Зачем ?Создание вашего первого приложения - Android

Вот мой код:

MyActivity.java

public class MyActivity extends AppCompatActivity 
{ 
    public final static String EXTRA_MESSAGE = "com.example.simon.helloworld.MESSAGE"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

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

DisplayMessageActivity.java

public class DisplayMessageActivity extends AppCompatActivity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null) 
         .show(); 
      } 
     }); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); 
     TextView textView = new TextView(this); 
     textView.setTextSize(40); 
     textView.setText(message); 

     RelativeLayout layout = (RelativeLayout) findViewById(android.R.id.content); 
     layout.addView(textView); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle app bar item clicks here. The app bar 
     // automatically handles clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment 
    { 

     public PlaceholderFragment() { } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.activity_display_message, 
        container, false); 
      return rootView; 
     } 
    } 

} 

content_my.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_my"> 

    <EditText android:id="@+id/edit_message" 
     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> 

content_display_message.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.simon.helloworld.DisplayMessageActivity" 
    tools:showIn="@layout/activity_display_message"> 

</RelativeLayout> 

Благодаря

enter image description here

+2

Где ваша трассировка ошибок журнала? – Rohit5k2

+2

опубликуйте свой логарифм –

+0

Вам нужен экран? Хорошо, я сделаю это;) – TotorAndMimine

ответ

3

Одна вещь, которую я вижу плохого в том, что вы делаете:

RelativeLayout layout = (RelativeLayout) findViewById(android.R.id.content); 

Вместо этого вы должны сделать:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.content); 

Еще один тонкий г, что вы могли бы сделать, это хорошая практика, чтобы добавить TextView в XML-RelativeLayout с имущества:

android:visibility:"gone" 

Тогда в DisplayMessageActivity вы делаете:

TextView textView = (TextView) findViewById(R.id.textView); 
textView.setText(message); 
textView.setVisibility(View.VISIBLE); 

Что касается ошибки, вы получаете, другие вещи могут быть неправильными. Нам нужно больше информации, чтобы знать наверняка.

+0

Спасибо, что работает сейчас;) – TotorAndMimine

Смежные вопросы