我的适配器出了什么问题?

时间:2014-05-06 04:54:31

标签: java android android-arrayadapter android-spinner oncreate

我正在尝试使用4个选项做一个简单的微调器,我创建了一个适配器并将其设置为微调器,但是当我用android模拟器测试它时只出现下一个错误:

05-06 00:43:56.948: E/AndroidRuntime(831): FATAL EXCEPTION: main
05-06 00:43:56.948: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to start activity ComponentInfo{economico.mx.uv/economico.mx.uv.MainActivity}: java.lang.NullPointerException
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:5103)
05-06 00:43:56.948: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 00:43:56.948: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:525)
05-06 00:43:56.948: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-06 00:43:56.948: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 00:43:56.948: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)
05-06 00:43:56.948: E/AndroidRuntime(831): Caused by: java.lang.NullPointerException
05-06 00:43:56.948: E/AndroidRuntime(831):  at economico.mx.uv.MainActivity.onCreate(MainActivity.java:36)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.Activity.performCreate(Activity.java:5133)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-06 00:43:56.948: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-06 00:43:56.948: E/AndroidRuntime(831):  ... 11 more

这是我的代码:

package economico.mx.uv;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
private Spinner criterioBusq;
private EditText camBusq;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        camBusq=(EditText)findViewById(R.id.camBusq);
        criterioBusq=(Spinner)findViewById(R.id.criterioBusq);
        String []opciones={"Campus","Facultad","Nombre","Cargo"};
        ArrayAdapter adaptador = new ArrayAdapter(this,android.R.layout.simple_spinner_item, opciones);
        criterioBusq.setAdapter(adaptador);
    }

我不知道我做错了什么,所以先谢谢你的阅读。

1 个答案:

答案 0 :(得分:0)

不要首先加载片段,因为它总是首先获取Bundle null,因此它将始终加载PlaceholderFragment,然后它将用于查找EditText和您的其他视图显然不在您的PlaceholderFragment布局文件中。所以它会抛出nullpointer错误。

试试如下:

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

       camBusq=(EditText)findViewById(R.id.camBusq);
        criterioBusq=(Spinner)findViewById(R.id.criterioBusq);
        String []opciones={"Campus","Facultad","Nombre","Cargo"};
        ArrayAdapter adaptador = new ArrayAdapter(this,android.R.layout.simple_spinner_item, opciones);
        criterioBusq.setAdapter(adaptador);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }