尝试在空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)'



基本上这个应用程序应该问你的名字,有一个保存的选项,当点击时,一个警告对话框弹出并问'你确定吗?',当点击yes时,它应该说'welcome + whatever name put'。我的问题是,这个应用程序一直在说欢迎之前就关闭了。我将字符串声明为userName并运行它,而没有对字符串执行任何函数,它只是说'welcome, null'。但当我这么做的时候用户名= editText.getText () .toString ();应用程序立即关闭。请帮帮我,我无计可施。调用欢迎页面的页面工作正常,但是welcome.java是有问题的文件。

public class Welcome extends Activity {
String userName;
final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    final TextView textView=(TextView)findViewById(R.id.textView);
    final EditText editText=(EditText)findViewById(R.id.editText);
    userName=editText.getText().toString();

    textView.setText(String.format("Welcome, %s.", userName));
}
}

logcat基本上是标题,给下面一行一个错误:

userName=editText.getText().toString();

PS我把那行代码移到了onCreate的前后,它仍然给出了错误

My MainActivity.java is

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
String[] menu={"User Preferences","Animation","Browser","Media","Take Picture"};
    setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.options, menu));
}

protected void onListItemClick(ListView l, View v, int position, long id){
    switch(position){
        case 0:
            startActivity(new Intent(MainActivity.this,userPreferences.class));
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
    }
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

我的activity_main.xml是

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<ImageView
    android:id="@+id/ic_launcher_sf"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:layout_marginTop="2px">
</ImageView>
<TextView
    android:id="@+id/options"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/travel"
    android:textSize="25sp"></TextView>

welcome.xml是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:textSize="30dp" />

我userpreferences.java

public class userPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userpreferences);
    Button save=(Button)findViewById(R.id.button);

  save.setOnClickListener(new View.OnClickListener() {
      final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);
      @Override
      public void onClick(View v) {
                  alertDialog.setTitle("Alert Dialog");
          alertDialog.setMessage("Are you sure you want to save?");
          alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          startActivity(new Intent(userPreferences.this,Welcome.class));
                      }
                  });
                  alertDialog.setNegativeButton("No", null);
                  alertDialog.show();

      }
  });
}

}

和我的用户首选项。xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:weightSum="1">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_weight="1"
    android:hint="Enter Your Name"
    android:textSize="20dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/button"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

问题出在这两方面

final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);

不要在没有调用setContentView()的情况下做findViewById,特别是在方法之外。让它:

private EditText editText;
private TextView textView;

也:在你的onCreate中,你不需要重新声明视图,只需要:

textView=(TextView)findViewById(R.id.textView);
editText=(EditText)findViewById(R.id.editText);

Plus:在welcome.xml中添加EditText

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:hint="THIS IS AN EDITTEXT" />

问题是,你有2个不同的视图在2个不同的xml从一个单一的活动调用。这就是为什么你会得到nullpointerexception。因此,将上下文作为一个额外的通过意图传递给所需的活动,并在findviewbyid方法中实现它。

相关内容

最新更新