不幸的是,当我使用onClickListener作为按钮时,app已停止

时间:2015-02-24 16:30:25

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

我不知道为什么我的代码无效。我正在关注如何向按钮添加监听器的教程,但我的应用程序甚至不再运行。我确定这是一个愚蠢的错误,但我已经被困了超过5个小时!这是我的代码:

MainActivity.java

package com.example.simplebuttons;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;



public class MainActivity extends ActionBarActivity {


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

        //System.out.print("HELLO");
        addListenerOnButton();

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

    public void addListenerOnButton() {
        Button b1 = (Button)findViewById(R.id.buttonGoStraight);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).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.main, 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();
        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_main, container, false);
            return rootView;
        }
    }

}

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black">

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a simple button app. \r\n \r\nCode Here \r\n"
        android:padding="10dp"
        android:textColor="#FFFFFF"/>

    <Button
        android:id="@+id/buttonGoStraight"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/go_straight"/>

    <Button
        android:id="@+id/buttonTurnLeft"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/turn_left"/>

    <Button
        android:id="@+id/buttonTurnRight"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/turn_right" />

    <Button
        android:id="@+id/buttonGoBack"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:text="@string/go_back" 
        android:layout_gravity="center_horizontal"/>

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="\n"/>

    <Button
        android:id="@+id/buttonGoBack"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:text="@string/destination" 
        android:gravity="center"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

activity_main.xml中

<FrameLayout 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"
    tools:context="com.example.simplebuttons.MainActivity"
    tools:ignore="MergeRootFrame" />

这是logcat: enter image description here

有没有人知道如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

因为带有buttonGoStraight ID的按钮位于fragment_main布局内PlaceholderFragment片段布局,因此请在addListenerOnButton中调用onCreateView并使用rootView访问视图:

    Button b1 = (Button)rootView.findViewById(R.id.buttonGoStraight);

答案 1 :(得分:1)

你试图在R.layout.activity_main上找到ViewById,但你的按钮位于片段的布局上......你应该在PlaceholderFragment的onCreateView中设置按钮的监听器

答案 2 :(得分:0)

在添加包含此元素的片段之前,您正在调用findViewById 。片段初始化后移动侦听器设置代码。