【问题标题】:NullPointerException when launching method in Fragment Class?在片段类中启动方法时出现 NullPointerException?
【发布时间】:2011-07-31 01:21:15
【问题描述】:
07-30 21:02:39.260: ERROR/AndroidRuntime(7961): java.lang.NullPointerException
07-30 21:02:39.260: ERROR/AndroidRuntime(7961):     at com.fttech.gameIT.shoppingClass$1.onClick(shoppingClass.java:45)
 07-30 21:02:39.260: ERROR/AndroidRuntime(7961):     at android.view.View.performClick(View.java:3110)
 07-30 21:02:39.260: ERROR/AndroidRuntime(7961):     at android.view.View$PerformClick.run(View.java:11928)
 07-30 21:02:39.260: ERROR/AndroidRuntime(7961):     at android.os.Handler.handleCallback(Handler.java:587)

这是我在启动另一个类时在这一行得到的代码 extends Fragment{

编辑:添加初始化

        shopping_details_fragment shopping;

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

    findIT = (Button)findViewById(R.id.findIT);
    shop = (EditText)findViewById(R.id.item);
    type = (RadioGroup)findViewById(R.id.console);
    site = (RadioGroup)findViewById(R.id.shopping_group);
    shopping = new shopping_details_fragment();

    final Intent d = new Intent(this, shopping_details_fragment.class);
    findIT.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            getUserPreference();
            shopping.loadUrl(url);// line 45
            startActivity(d);


        }

这是我正在尝试启动的活动

public class shopping_details_fragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    //Return the view for our WebView
    return(inflater.inflate(R.id.browserFrag,container, false));

}

public void loadUrl(String url){
    ((WebView)getView().findViewById(R.id.browser)).loadUrl(url);
}

}

编辑 2:它现在给我一个错误

((WebView)getView().findViewById(R.id.browser)).loadUrl(url);

这是错误

 07-30 22:34:42.820: ERROR/AndroidRuntime(9785): java.lang.NullPointerException
 07-30 22:34:42.820: ERROR/AndroidRuntime(9785):     at com.fttech.gameIT.shopping_details_fragment.loadUrl(shopping_details_fragment.java:21)
07-30 22:34:42.820: ERROR/AndroidRuntime(9785):     at com.fttech.gameIT.shoppingClass$1.onClick(shoppingClass.java:50)
07-30 22:34:42.820: ERROR/AndroidRuntime(9785):     at android.view.View.performClick(View.java:3110)
07-30 22:34:42.820: ERROR/AndroidRuntime(9785):     at android.view.View$PerformClick.run(View.java:11928)
07-30 22:34:42.820: ERROR/AndroidRuntime(9785):     at android.os.Handler.handleCallback(Handler.java:587)

编辑 3:detials_fragment.xml 这个 xml 持有带有 id 浏览器的 WebView...

<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:id="@+id/browser"
 android:layout_height="match_parent">
</WebView>

这里是保存片段 browserFrag 的购物 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
 android:layout_height="fill_parent"

>
<LinearLayout

android:name="com.fttech"
android:orientation="vertical"
android:layout_width="525dp" android:layout_height="match_parent" android:weightSum="1">

  <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" android:orientation="vertical">
    <Button android:text="Find IT!" android:id="@+id/findIT" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
   </LinearLayout>
   <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/item"></EditText>
   <TextView android:text="Pick Your Shopping Site" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="21dip"> </TextView>

  </LinearLayout>

<fragment android:layout_height="match_parent" android:id="@+id/browserFrag" android:name="com.fttech.gameIT.shopping_details_fragment" android:layout_width="match_parent">  </fragment>

 </LinearLayout>

【问题讨论】:

    标签: android nullpointerexception fragment


    【解决方案1】:

    我将做出一个疯狂的假设,shoppingClass.java 的第 45 行是:

    shopping.loadUrl(url);
    

    我还要猜测shopping 没有被正确初始化。

    编辑:如果将上面的行替换为:

    if (shopping == null) {
        Toast.makeText(this, "shopping==null!", Toast.LENGTH_SHORT).show();
    } else {
        shopping.loadUrl(url);
    }
    

    如果您收到 toast 消息,那么您可能正在代码中的其他位置将 null 分配给 shopping。如果您在购物时设置了写监视,您的调试器可以提供帮助。

    【讨论】:

    • 在完成调试建议后,它在上面的第二次编辑中给出了错误
    • 似乎没有找到 ID 为 browser 的字段。片段是否正确膨胀? (仅使用new 构建一个并不能完成这项工作。)您可以发布 shopping.xml 和片段的 xml 吗?
    • 上面是xml。如您所见,购物 xml 在右侧有片段,在左侧有一些 wigdets。 details_fragment 持有我在 loadUrl() 中膨胀的 WebView
    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多