java - OnClickListener 中的空指针异常

标签 java android

我对 Android 还很陌生。 我有以下代码来在用户单击按钮时显示自定义对话框。 这是代码:

MainActivity.java

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

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

    Button customBtn = (Button)findViewById(R.id.custombtn);
    customBtn.setOnClickListener(this);
}


@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);
}

@Override
public void onClick(View v)
{
    if(v == findViewById(R.id.custombtn))
    {
        String message = "This is a Custom Dialog.";

        Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.custom_layout);
        dialog.setTitle("Custom Dialog");

        TextView texter = (TextView)findViewById(R.id.text);
        texter.setText(message);

        ImageView image = (ImageView)findViewById(R.id.image);
        //image.setImageResource(R.drawable.abc_spinner_mtrl_am_alpha);
        dialog.show();
    }
}
}

这是 custom_layout.xml 文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="@color/accent_material_light"/>
</LinearLayout>

每当我运行应用程序并单击自定义按钮时,应用程序都会崩溃并出现以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.adhish.onclicklisten.MainActivity.onClick(MainActivity.java:105)

我对 Android 还很陌生,不知道这意味着什么。我尝试过谷歌搜索,但对我来说不起作用。

请帮忙。

最佳答案

使用dialogcustom_layout布局访问 View :

    TextView texter = (TextView)dialog.findViewById(R.id.text);
    texter.setText(message);
    ImageView image = (ImageView)dialog.findViewById(R.id.image); 

关于java - OnClickListener 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779593/

相关文章:

java - 刷新对象后出现 StaleObjectStateException

java - Android展开JNI本地引用表

android - 如何从 Android 选项卡内的 Activity 中禁用选项卡

android - 普通字体在某些设备上显示为斜体。为什么以及如何解决?

java - StringBuilder.append() 导致 java.lang.NullPointerException

java - Sonar 管 5.6 : violations reported for an outer class by findbugs is not reported as a sonar violation

java - 静态方法的行为与其他可以重写的方法类似

android - FCM Firebase 推送通知 Android/iOS

android - 忽略 navigate() 调用 : FragmentManager has already saved its state

android - 未初始化的属性访问异常 : lateinit property has not been initialized