构建您的第一个应用程序 - Android

问题描述:

当我将模拟器和应用程序拉开后,我有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

您的logact错误跟踪在哪里? – Rohit5k2

+2

发布你的logcat –

+0

你想要一个屏幕?好吧,我会做到这一点;) – TotorAndMimine

一两件事,我看到的是错的是你做的:

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

相反,你应该做的:

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

另一个薄摹,你可以做的,这是一个很好的做法,是与财产添加的TextView中的RelativeLayout的XML:

android:visibility:"gone" 

然后,在你做的DisplayMessageActivity:

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

关于错误你会得到,其他的事情可能是错的。我们需要更多的信息来确认。

+0

感谢它现在的作品;) – TotorAndMimine