Android碎片:立即空点异常

时间:2013-09-17 21:43:57

标签: android android-fragments

我知道这些信息都在互联网上,但我不能为我的生活弄清楚我做错了什么。当我尝试运行我的代码时,它立即崩溃并出现空指针异常,但我似乎无法弄清楚为什么会发生这种情况或发生的情况。我想我错过了一些相当简单的东西,但我是android的新手,所以我不知道它是什么。这是代码:

MainActivity:

public class MainActivity extends Activity implements GetConnInfoFragment.ConnInfoReceiver {

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

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    GetConnInfoFragment frag = new GetConnInfoFragment();
    fragmentTransaction.add(R.id.mainFragmentContainer, frag);
    fragmentTransaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
    ....

GetConnInfoFragment:

public class GetConnInfoFragment extends Fragment {
ConnInfoReceiver ownerActivity; 

@SuppressLint("NewApi")
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button goButton = (Button) getView().findViewById(R.id.connectButton);
    goButton.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
            EditText portBox = (EditText) v.findViewById(R.id.portBox);
            int port = Integer.parseInt(portBox.getText().toString());
            EditText ipBox = (EditText) v.findViewById(R.id.ipAddressBox);
            String address = ipBox.getText().toString();

            // create sockets, might these be garbage collected ? 
            // in which case the main activity should create them. That makes
            // more sense anyway
            // activate next fragment -- call back up to the main activity to trigger this
            ownerActivity.receiveConnInfo(port, address);
        }
    });

    return inflater.inflate(R.layout.getconninfofragment,  container, false);
}

以下是错误消息:

E/AndroidRuntime(1470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.htpccontrol/com.htpccontrol.MainActivity}: java.lang.NullPointerException

编辑:这是我的getConnInfoFragment.xml的<.xml文件

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

<EditText
    android:id="@+id/ipAddressBox"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:hint="@string/enterIP" />

    <EditText
        android:id="@+id/portBox"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:hint="@string/enterPort" />

    <Button 
        android:id="@+id/connectButton"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:text="@string/Connect" />


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

goButton的onClick实例化EditText。在下一行中,您将文本解析为Integer。 EditText的新实例没有文本,因此portBox.getText().toString()返回null。 ipBox EditText也会发生同样的情况。

让用户在EditText中输入内容,使用portBox.setText()在其中放置文本,或者使用android:text="yourText"将文本设置为xml。否则代码没有意义。因为EditText的范围仅在onClick方法中。所以你不能在方法之外使用它。

要在解析时摆脱NPE,请检查null:

if(portBox.getText() != null){
    port = Integer.parseInt(portBox.getText().toString());
}

编辑:

您的代码还有一些缺陷。试试这段代码:

// Class fields
EditText portBox;
EditText ipBox;
@SuppressLint("NewApi")
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // inflate the layout
    View view = inflater.inflate(R.layout.getconninfofragment,  null, false);

    // get the Views of your inflated layout
    Button goButton = (Button) view.findViewById(R.id.connectButton); 
    portBox = (EditText) v.findViewById(R.id.portBox); // set a text to it like I said
    ipBox = (EditText) v.findViewById(R.id.ipAddressBox);

    goButton.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
           int port;
           String address;

           if(portBox.getText() != null && ipBox.getText != null){
                port = Integer.parseInt(portBox.getText().toString());
                address = ipBox.getText().toString();
                // check if this is valid, I can't see it in the snippets of your code
                ownerActivity.receiveConnInfo(port, address);
           }
        }
    });

    return view; // return the view you inflated
}

答案 1 :(得分:0)

很难对此进行远程调试,但是您应该检查是否所有变量(例如ownerActivity)都已初始化,或者是否portBox.getText()可能返回null。

一个好方法是设置一个断点你创建的代码并启动应用程序进行调试(在Eclipse中使用“Debug as”而不是“Run as”)。这将让您单步执行代码以找到导致异常的行。