添加onClickListener Android时,应用程序崩溃

时间:2014-04-24 05:55:55

标签: android onclicklistener buttonclick

我不知道为什么,但我似乎无法弄清楚我的代码有什么问题。只要我添加一个onclickListener,应用程序就会崩溃。我真的需要帮助。我也看了其他线索,但我似乎无法找出我做错了什么。谢谢  代码如下:

package com.example.medrec;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements OnClickListener{

Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

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


    login = (Button)findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);
}





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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

我的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.medrec.MainActivity"
tools:ignore="MergeRootFrame" />

我的fragment_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.medrec.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/w_number_edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="21dp"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="118dp"
    android:text="@string/w_number" />

<Button
    android:id="@+id/w_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/w_number_edit_text"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:text="@string/w_login_text" />

5 个答案:

答案 0 :(得分:3)

尝试以下代码: -

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

    login = (Button)rootView.findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);
        return rootView;
    }
}

答案 1 :(得分:2)

您的应用已崩溃,因为您的按钮 w_login 属于您的fragment_main.xml,您必须将activity_main.xml布局设置为Activity。< / p>

代码

login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);

您应在Button

中引用此Fragment onCreateView(......)

答案 2 :(得分:2)

我认为它崩溃是因为你的登录名为null,因为它是null,因为你的活动布局中不存在w_login。试试这个(片段中的rootView),

login = (Button) rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);

答案 3 :(得分:1)

  assign on click listener in  onCreateView

   login = (Button) rootView.findViewById(R.id.w_login);
        //Crashes when I add this line.
      login.setOnClickListener(this);

答案 4 :(得分:1)

您的应用在搜索NullPointerException中的Button时显示activity_main.xml但仍然无法找到。相反,您应该在PlaceholderFragment片段中添加按钮代码。

MainActivity

中删除以下行
login = (Button)findViewById(R.id.w_login);
    //Crashes when I add this line.
    login.setOnClickListener(this);

将以下内容添加到PlaceholderFragment

public static class PlaceholderFragment extends Fragment implements OnClickListener{

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        Button login = (Button)rootView.findViewById(R.id.w_login);
        login.setOnClickListener(this);
        return rootView;
    }
}