尝试通过添加新活动和按钮来编辑Android教程应用程序,但是在按下"发送"按钮我一直收到运行时异常错误(应用程序崩溃)。已经查看了许多类似的问题,但是为别人工作的解决方案对我不起作用。
DisplayMessageActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myfirstapp.DisplayMessageActivity"
tools:ignore="MergeRootFrame" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:onClick="switchActivity"
android:text="@string/next" />
</LinearLayout>
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
public 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);
ViewGroup layout = (ViewGroup) findViewById(R.id.container);
layout.addView(textView);
// Set the text view as the activity layout
setContentView(R.layout.activity_display_message);
//setContentView(textView);
}
@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();
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.fragment_display_message,
container, false);
return rootView;
}
}
public void switchActivity (View view) {
Intent maxIntent = new Intent(this, Selection.class);
startActivity(maxIntent);
}
}
希望你能找到我的错误。 提前谢谢!
答案 0 :(得分:5)
您应该在
之前移动setContentView(R.layout.activity_display_message);
ViewGroup layout = (ViewGroup) findViewById(R.id.container);
喜欢这样,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
ViewGroup layout = (ViewGroup) findViewById(R.id.container);
.............
............
...........
}
并尝试使用此方式在布局中添加视图。
LinearLayout layout= (LinearLayout)findViewById(R.id.container);
layout.addView(textView);