尝试在空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)' [英] Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

查看:75
本文介绍了尝试在空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上这个应用程序应该询问您的名字,并带有一个保存选项,单击该选项时,将弹出一个警告对话框,询问您确定吗?",单击是时,它应该显示"welcome + what"名称".我的问题是该应用在显示欢迎"之前一直关闭.我将字符串声明为userName,并且对该字符串运行时没有任何功能,它只是说"welcome,null".但是当我做的时候userName = editText.getText().toString();该应用程序立即关闭.请帮助我没有主意.调用欢迎页面的页面可以正常工作,但是welcome.java是存在问题的文件.

Basically this app is supposed to ask for your name, with an option to save, which when click, an alert dialog pops up and asks 'are you sure?', when yes is clicked, it should say 'welcome + whatever name put'. my problem is that the app keeps shutting down before it says welcome. I declared the string as userName and ran it without any function to the string, and it just said 'welcome, null'. but when i did userName=editText.getText().toString(); the app shut down immediately. Please HELP I'm out of ideas. the page which calls the welcome page works fine, but the welcome.java is the file with the issue.

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基本上是标题,这使以下行出现错误:

The logcat is basically the title, giving an error to the following line:

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

PS我在onCreate之前和之后移动了那行代码,它仍然给出错误

PS I've moved that line of code before and after onCreate and it still gave errors

我的MainActivity.java是

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是

My activity_main.xml is

    <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是

My welcome.xml is

<?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

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


      }
  });

}

}

和我的用户preferences.xml文件

and my user preferences.xml file

<?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.做到:

Do not do findViewById without setContentView() called, or especially outside the methods. Make it:

private EditText editText;
private TextView textView;

还有:在您的onCreate内,您不必重新声明视图就可以了:

And also: inside your onCreate, you don't have to redeclare the views just do:

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

Plus:在您的welcome.xml上添加一个EditText

Plus: Add an EditText on your welcome.xml

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

这篇关于尝试在空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆