单击按钮后启动新活动

时间:2013-12-26 14:34:58

标签: java android android-fragments android-activity android-studio

我是Android新手,我正在关注示例here。我可以使布局正常,并且按钮事件会触发,但Android Studio似乎与Eclipse在其示例中的工作方式不同。

我添加了一个新的Activity,但Android Studio也强制添加一个新的Fragment。我已经阅读了Fragments,但不明白为什么我们被迫在AS中使用它而不是在Eclipse中使用它。无论哪种方式,这似乎打破了我的应用程序。当我点击按钮时出现错误

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {} com.t.at/com.t.at.DisplayMessageActivity:   java.lang.IllegalArgumentException:找不到id 0x7f07003c的视图   片段(com.tutorials.androidtutorials:id / container)   PlaceholderFragment {b1e1f5e8#0 id = 0x7f07003c}

我确信我已经按照教程所说的一切,但我看到的唯一区别是AS总是创建的片段文件。单击按钮后如何启动新活动?这是我的点击处理程序

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

我的activity_displaymessage.xml文件如下所示

<FrameLayout 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"
    tools:context=".DisplayMessageActivity"
    tools:ignore="MergeRootFrame" />

和fragment_displaymessage.xml

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

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

根据评论中的要求,这是片段代码

public class DisplayMessageActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, 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.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                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_displaymessage, container, false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:3)

No view found for id 0x7f07003c (com.tutorials.androidtutorials:id/container) for fragment PlaceholderFragment{b1e1f5e8 #0 id=0x7f07003c}

activity_displaymessage.xml中没有ID为R.id.container

的视图
  if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

你需要

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container