当前位置: 代码迷 >> Android >> ,findViewById()方法返回空
  详细解决方案

,findViewById()方法返回空

热度:105   发布时间:2016-05-01 13:27:54.0
求助,findViewById()方法返回空
最近在测试android文件读写的时候遇到的一个问题:findviewbyid()方法总是返回null.
先贴源代码:
主activity:
package com.android.file;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.filecontrol.FileService;

public class AndroidfileActivity extends Activity {
/** Called when the activity is first created. */
private Button saveBtn, getBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

saveBtn = (Button) findViewById(R.id.button1);
getBtn = (Button) findViewById(R.id.button2);
saveBtn.setOnClickListener(onclick);
getBtn.setOnClickListener(onclick);
}

public OnClickListener onclick = new OnClickListener() {
FileService fileService = new FileService();
EditText editfileEditText = (EditText) findViewById(R.id.edittext1);
EditText editcontentEditText = (EditText) findViewById(R.id.edittext2);
  TextView textview = (TextView)findViewById(R.id.textresult);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
try {
fileService.WriteFile(AndroidfileActivity.this.openFileOutput(
editfileEditText.getText().toString(), MODE_APPEND),
editcontentEditText.getText().toString());
Toast.makeText(AndroidfileActivity.this, "保存成功!", Toast.LENGTH_LONG);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.button2:
try {
String contentString = fileService.ReadFile(AndroidfileActivity.this.openFileInput(editfileEditText.getText().toString()));
textview.setText(contentString);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
};
}

layout下的main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:android1="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >

  <TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="文件名" 
  />
  <EditText
  android:id="@+id/edittext1"
  android:layout_width="330dp"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@id/textView1"
  android:layout_alignParentTop="true"
  android:layout_toRightOf="@id/textView1"
  android:layout_weight="0.10" 
  />

  <TextView
  android:id="@+id/textView2"