Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编辑文本时出现空指针异常_Java_Android_Nullpointerexception - Fatal编程技术网

Java 编辑文本时出现空指针异常

Java 编辑文本时出现空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在编写一个程序来保存编辑文本中的用户输入,并将文本插入listview。即使我已经声明了编辑文本,我仍然会得到这个空异常 public class AddEditAlbum extends AppCompatActivity { /** * These keys are to send back and forth information between the bundles and intents */ public static final String ALBUM_INDEX

我正在编写一个程序来保存编辑文本中的用户输入,并将文本插入listview。即使我已经声明了编辑文本,我仍然会得到这个空异常

public class AddEditAlbum extends AppCompatActivity {

/**
 * These keys are to send back and forth information between the bundles and intents
 */
public static final String ALBUM_INDEX = "albumIndex";
public static final String ALBUM_NAME = "albumName";
EditText input;
Button save, cancel;
int albumIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {

    save = (Button) findViewById(R.id.save);
    cancel = (Button) findViewById(R.id.cancel);
    input = (EditText) findViewById(R.id.add);

    // see if info was passed in to populate field
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        albumIndex = bundle.getInt(ALBUM_INDEX);
        input.setText(bundle.getString(ALBUM_NAME));
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_album);
}

public void Cancel(View view) {
    setResult(RESULT_CANCELED);
    finish();   //Returns to previous page on call stack
}

public void addAlbum(View view){

    String name = input.getText().toString();   //Fix this, goes to null pointer

    //Checks to see if input is null and returns
    if(name == null || name.length()==0){
        Toast.makeText(AddEditAlbum.this, "Enter valid album name", Toast.LENGTH_SHORT).show();
        Bundle bundle = new Bundle();
        bundle.putString(AlbumDialog.MESSAGE_KEY, "Album Name Required");
        DialogFragment newFragment = new AlbumDialog();
        newFragment.setArguments(bundle);
        newFragment.show(getFragmentManager(), "badfields");
        return;
    }
    //Toast.makeText(AddEditAlbum.this, "Enter valid album name", Toast.LENGTH_SHORT).show();
    Bundle bundle = new Bundle();
    bundle.putInt(ALBUM_INDEX, albumIndex);
    bundle.putString(ALBUM_NAME, name);

    // send back to caller
    Intent intent = new Intent();
    intent.putExtras(bundle);
    setResult(RESULT_OK,intent);
    finish();
}
}

这必须首先执行,否则contentView未设置,findViewById将找不到任何内容,导致EditText为null

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.album_list);

}

另外,您正在调用onCreate方法之前的listview,从而给您一个NPE,这是com.example.mustu.androidphotos31.AddEditAlbum.addAlbum(AddEditAlbum.java:54)@AlLelopath OP在代码中提到的
字符串名称=input.getText().toString()
,哪一行是
addAlbum()
method.oops,Mymistake@mous输入为空。NPE是由于试图在null上调用getText而导致的object@mousstacktrace声明,
input
为空。我在6分钟前已经说过了。重复它不是useful@TimCastelijns哎呀。。我错过了我6分钟前已经说过了。重复它是没有用的我正在打ans,然后看到:(非常感谢你,它工作了!
update your onCreate() like this.
we should call setContentView(R.layout.your_layout) for passing the layout to the java class, then only it's views can be used.. failing to do so, will lead to NPE. 

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

    save = (Button) findViewById(R.id.save);
    cancel = (Button) findViewById(R.id.cancel);
    input = (EditText) findViewById(R.id.add);

    // see if info was passed in to populate field
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        albumIndex = bundle.getInt(ALBUM_INDEX);
        input.setText(bundle.getString(ALBUM_NAME));
    }


}
super.onCreate(savedInstanceState);
setContentView(R.layout.add_album);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.album_list);

}
update your onCreate() like this.
we should call setContentView(R.layout.your_layout) for passing the layout to the java class, then only it's views can be used.. failing to do so, will lead to NPE. 

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

    save = (Button) findViewById(R.id.save);
    cancel = (Button) findViewById(R.id.cancel);
    input = (EditText) findViewById(R.id.add);

    // see if info was passed in to populate field
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        albumIndex = bundle.getInt(ALBUM_INDEX);
        input.setText(bundle.getString(ALBUM_NAME));
    }


}