java - 为按钮创建类但出现 Null Exception 错误

标签 java android

我正在尝试创建一个按钮类,该类创建的按钮在单击时会向手机浏览器激发 Intent ,但在我的设备上启动时收到 java.lang.NullPointerException 。我读到这可能是由 findViewById 返回 null 引起的,因为它引用了错误的 View 。我一直在寻找一种将 R 从 ItemActivity 传递到 ButtonInternetAcces 的方法,但我读到这是没有必要的。 eclipse 中没有错误,所以我很困惑如何解决该问题。

这是可能导致错误的行:

按钮 goNextBtn = (Button) findViewById(R.id.internet_button);

ButtonInternetAccess:

public class ButtonInternetAccess extends Activity{

    public Button main(final String url){

        Button goNextBtn = (Button) findViewById(R.id.internet_button);

        OnClickListener goToNet = new OnClickListener(){

        @Override
        public void onClick(View v){
            Intent net = new Intent(Intent.ACTION_VIEW);
            net.setData(Uri.parse(url));
            startActivity(net);
        }

        };

        goNextBtn.setOnClickListener(goToNet);

        return goNextBtn;
    }
}

Activity 类ItemActivity

public class ItemActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);     
        TextView tvResponse = (TextView) findViewById(R.id.response);
        Intent i = getIntent();
        Bundle b = i.getExtras();
        String mid = b.getString("mid");
        tvResponse.setText(mid);

        ButtonInternetAccess bia = new ButtonInternetAccess();

        Button goNextBtn = bia.main("http://www.google.com");

    }
     //............ 

}

布局/activity_item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.app.ItemActivity" >

    <TextView
        android:id="@+id/response"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/second.welcome" />

    <Button
        android:id="@+id/internet_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/response"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="Button" />

</RelativeLayout>

错误日志:

02-19 08:24:06.735: E/AndroidRuntime(9802): FATAL EXCEPTION: main
02-19 08:24:06.735: E/AndroidRuntime(9802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.ItemActivity}: java.lang.NullPointerException
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.os.Looper.loop(Looper.java:137)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at java.lang.reflect.Method.invoke(Method.java:511)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at dalvik.system.NativeStart.main(Native Method)
02-19 08:24:06.735: E/AndroidRuntime(9802): Caused by: java.lang.NullPointerException
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.Activity.findViewById(Activity.java:1825)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at com.component.ButtonInternetAccess.main(ButtonInternetAccess.java:16)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at com.app.ItemActivity.onCreate(ItemActivity.java:30)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.Activity.performCreate(Activity.java:5008)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-19 08:24:06.735: E/AndroidRuntime(9802):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

最佳答案

假设这就是layout/activity_item中的所有代码,问题就在这里:

setContentView(R.layout.activity_item);     
TextView tvResponse = (TextView) findViewById(R.id.response);

第一行将您的layout/activity_item.xml指定为ItemActivity的布局。第二个查看该布局文件以查找 id 为“response”的 View 。但是,没有任何 View ,因为该布局中的唯一 View 是 ID 为“internet_button”的按钮。

我也很好奇,你想创建一个制作按钮的类有什么原因吗?您可以创建一个方法来执行相同的操作并返回 Button。

更新:

@Override
protected void onCreate(Bundle savedInstanceState) {  //put the code you had in main() here, after these two lines
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item); //this should be the layout for this class; in this layout you should have the internet button
    ...
}

但是,如果您想要重用该 Button,那么也许您应该让您的类扩展 Button 而不是 Activity。这样您就可以在需要时简单地实例化该按钮。查看this发帖看看如何做到这一点:

关于java - 为按钮创建类但出现 Null Exception 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495290/

相关文章:

android - 如何在android中获取应用程序的名称?

java - 在 Android 中搜索带有关键字的任何网页

java - 将二叉树展平为链表

java - 非静态内部类名作为类型参数

android - 奇怪!!,Textview 值正在从后台线程更新

Android ImageView坐标点击

java - 是否可以在没有生成查询类型的情况下使用 Querydsl?

java - GSON 默认日期序列化器是特定于语言环境的

java - 尝试使用 servlet + jsp + mysql 将多个文件(具有任何扩展名)上传到 mysql 数据库

android - 从 webservice 对象获取数组项到 android