为什么我的应用程序停止工作?

时间:2014-03-22 21:45:18

标签: android button android-edittext fatal-error

我试图在模拟器上运行这个简单的代码,但它在开头就给出了错误。帮助赞赏。它在logcat中给出致命错误。它说"应用已停止"。我收到这些错误: " 03-22 17:43:53.315:E / AndroidRuntime(1283):处理:com.example.deneme,PID:1283" " 03-22 17:43:53.315:E / AndroidRuntime(1283):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.deneme / com.example.deneme.MainActivity}:java.lang .NullPointerException"

package com.example.deneme;
import android.app.Activity;
import android.app.ActionBar;
import android.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.widget.Button;
import android.widget.EditText;
import android.os.Build;

public class MainActivity extends Activity {

 final Button button = (Button) findViewById(R.id.button1);
 EditText txt = (EditText) findViewById (R.id.editText1);

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

        button.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   txt.setText("murat");
   }
  });
       // if (savedInstanceState == null) {
         //   getFragmentManager().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.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;
        }
    }

}

4 个答案:

答案 0 :(得分:1)

button创建视图之前,您要为txtonCreate()分配值。在调用setContentView(R.layout.activity_main)之前,您不应该分配它们。

答案 1 :(得分:0)

您需要将按钮和textview放在onCreate中。

答案 2 :(得分:0)

尝试以下

public class MainActivity extends Activity {

 final Button button; // Don't call find view by ID here;
 EditText txt; // Don't call find view by ID here;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (EditText) findViewById (R.id.editText1); // Instead call it after setContentView
        button = (Button) findViewById(R.id.button1); // Instead call it after setContentView
        button.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   txt.setText("murat");
   }
  });
       // if (savedInstanceState == null) {
         //   getFragmentManager().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.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;
        }
    }

}

答案 3 :(得分:0)

这些行会给你错误:

final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);

您无法在任何方法之外初始化ButtonEditText,因为尚未设置布局。但是,您可以在任何方法之外声明它们,然后在设置布局(视图)之后onCreate方法中初始化它们。像这样:

// declare them at a class level
Button button;
EditText txt;

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

    // initialize them here
    button = (Button) findViewById(R.id.button1);
    txt = (EditText) findViewById (R.id.editText1);

    button.setOnClickListener(new View.OnClickListener() {
    ...

然而,button不能是final