ArrayAdapter上的java.lang.NullPointerException

ArrayAdapter上的java.lang.NullPointerException,java,android,listview,android-arrayadapter,Java,Android,Listview,Android Arrayadapter,我有两个布局,一个有editText,另一个有一个listview来更改editText的字体 该应用程序在单击按钮打开listView布局时崩溃,可能是因为阵列适配器 请帮帮我 每个按钮都工作正常。只有btnChangeFont按钮有问题 我的代码: MainActivity.java[enter image description here][1] package com.example.xarn.test; public class MainActivity extends Acti

我有两个布局,一个有editText,另一个有一个listview来更改editText的字体

该应用程序在单击按钮打开listView布局时崩溃,可能是因为阵列适配器

请帮帮我

每个按钮都工作正常。只有btnChangeFont按钮有问题

我的代码:

MainActivity.java[enter image description here][1]
package com.example.xarn.test;



public class MainActivity extends ActionBarActivity {

    String[] values = new String[]{

            "art_science",
            "big_bold",
            "bold_itallic",
            "crazy_days",
            "gabriola",
            "handwritting",
            "new_bold",
            "new_itallic",
            "oldschool",
            "rough&tough",
            "sketch",

    };

    ListView listView;
    EditText text;
    LinearLayout choosefonts;
    ImageButton btnOpen, btnClose, btnNew, btnSave, btnDelete,btnChangeFont;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText)findViewById(R.id.text);
        listView =(ListView)findViewById(R.id.listView);
        btnChangeFont = (ImageButton)findViewById(R.id.btnChangeFont);
        btnChangeFont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.choosefonts);
                choosefonts = (LinearLayout)findViewById(R.id.choosefonts);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,values);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    switch (position){
                        case 0:text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
                             break;
                        case 1: text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/big_bold.ttf"));
                            break;
                        case 3:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/bold_itallic.ttf"));
                            break;
                        case 4:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/crazy_days.ttf"));
                            break;
                        case 5:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/gabriola.ttf"));
                            break;
                        case 6:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/handwritting.ttf"));
                            break;
                        case 7:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_bold.ttf"));
                            break;
                        case 8:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_itallic.ttf"));
                            break;
                        case 9:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/oldschool.ttf"));
                            break;
                        case 10:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/rough&tough.ttf"));
                            break;
                        case 11:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sketch.ttf"));
                            break;}
                }
            });

            }
        });


        text.setHint("Write Your Note Here............");
        text.setTextIsSelectable(true);

        btnSave = (ImageButton)findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog d = new Dialog(MainActivity.this);
                d.setTitle("Save As");
                d.setContentView(R.layout.dialogsave);
                d.show();
                Button btnDialogSave = (Button)d.findViewById(R.id.btnDialogSave);
                btnDialogSave.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText saveText = (EditText)d.findViewById(R.id.SaveText);
                        try {
                            File myFile = new File("/sdcard/"+saveText.getText()+".txt");
                            myFile.createNewFile();
                            FileOutputStream fOut = new FileOutputStream(myFile);
                            OutputStreamWriter myOutWriter =
                                    new OutputStreamWriter(fOut);
                            myOutWriter.append(text.getText());
                            myOutWriter.close();
                            fOut.close();
                            Toast.makeText(getBaseContext(),
                                    ""+saveText.getText()+" Saved",
                                    Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            Toast.makeText(getBaseContext(), e.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }
                        d.dismiss();
                    }
                });
            }
        });
        btnNew = (ImageButton)findViewById(R.id.btnNew);
        btnNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("");
            }
        });
        btnClose = (ImageButton)findViewById(R.id.btnClose);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnOpen = (ImageButton)findViewById(R.id.btnOpen);
        btnOpen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                final Dialog d2 = new Dialog(MainActivity.this);
                d2.setTitle("Open");
                d2.setContentView(R.layout.dialogopen);
                d2.show();
                Button btnDialogOpen = (Button)d2.findViewById(R.id.btnDialogOpen);
                btnDialogOpen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText openText = (EditText)d2.findViewById(R.id.OpenText);
                        try {
                            File myFile = new File("/sdcard/"+openText.getText()+".txt");
                            FileInputStream fIn = new FileInputStream(myFile);
                            BufferedReader myReader = new BufferedReader(
                                    new InputStreamReader(fIn));
                            String aDataRow = "";
                            String aBuffer = "";
                            while ((aDataRow = myReader.readLine()) != null) {
                                aBuffer += aDataRow + "\n";
                            }
                            text.setText(aBuffer);
                            myReader.close();
                            Toast.makeText(getBaseContext(),
                                    ""+openText.getText()+" Opened",
                                    Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            Toast.makeText(getBaseContext(), e.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }
            d2.dismiss();

                    }

                });

            }
        });

    }



    @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;
        } else if(id == R.id.changeFont){
           final Dialog d3 = new Dialog(MainActivity.this);
            d3.setContentView(R.layout.choosefonts);
            d3.setTitle("Choose Fonts");
            d3.show();
          /*  String[] values = new String[]{
                    "art_science",
                    "big_bold",
                    "bold_itallic",
                    "crazy_days",
                    "gabriola",
                    "handwritting",
                    "new_bold",
                    "new_itallic",
                    "oldschool",
                    "rough&tough",
                    "sketch",

            };
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,values);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    switch (position){case 0:text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
                        // text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/art_science.ttf"));
                        break;
                        case 1: text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/big_bold.ttf"));
                            break;
                        case 3:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/bold_itallic.ttf"));
                            break;
                        case 4:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/crazy_days.ttf"));
                            break;
                        case 5:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/gabriola.ttf"));
                            break;
                        case 6:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/handwritting.ttf"));
                            break;
                        case 7:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_bold.ttf"));
                            break;
                        case 8:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/new_itallic.ttf"));
                            break;
                        case 9:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/oldschool.ttf"));
                            break;
                        case 10:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/rough&tough.ttf"));
                            break;
                        case 11:text.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sketch.ttf"));
                            break;}
                }
            });*/
        }


        return super.onOptionsItemSelected(item);
    }
}

我相信您忘了将名为“text”的EditText视图分配给xml布局中的匹配视图。

变量
text
已定义,但从未分配过值。

是否可以添加logcat错误,请特别查找它崩溃的行号在我的情况下,我无法获取指向null的行号。。我是安卓工作室的新手,但上了两年java课程。在netbeansLogcat中,还将检查是否已实例化所有变量和对象。如果你对azurefrog不太了解,请阅读文章建议。它是指定的。。但我不知道是什么错误。我认为这是arrayadapter的BCU。我把ArrayAdapter(这个,诸如此类,诸如此类)的颜色显示为红色。wn我将其替换为wd main activity.this。应用程序崩溃。我还认为MainActivity未与包含listview的其他布局正确链接。但是我不知道它是如何分配的。。但我不知道是什么错误。我认为这是arrayadapter的BCU。我把ArrayAdapter(这个,诸如此类,诸如此类)的颜色显示为红色。wn我将其替换为wd main activity.this。应用程序崩溃。我还认为MainActivity没有与包含listview的其他布局正确链接。但我不知道怎么做
<RelativeLayout 

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:height="200pt"
        android:gravity="top"
        android:scrollbars="horizontal"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnNew"
        android:layout_below="@+id/text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/new1" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnSave"
        android:layout_alignBottom="@+id/btnNew"
        android:layout_toRightOf="@+id/btnNew"
        android:layout_toEndOf="@+id/btnNew"
        android:background="@drawable/save"
        android:clickable="true" />


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnOpen"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/btnSave"
        android:layout_toEndOf="@+id/btnSave"
        android:background="@drawable/open4"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnClose"
        android:background="@drawable/close"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/btnOpen"
        android:layout_toEndOf="@+id/btnOpen" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnDelete"
        android:background="@drawable/delete"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/btnClose"
        android:layout_toEndOf="@+id/btnClose" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnChangeFont"
        android:layout_alignBottom="@+id/btnDelete"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/btnDelete"
        android:background="@drawable/star"/>


<RelativeLayout/>
06-22 11:48:18.188    8058-8058/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.xarn.test, PID: 8058
    java.lang.NullPointerException
            at com.example.xarn.test.MainActivity$1.onClick(MainActivity.java:65)
            at android.view.View.performClick(View.java:4443)
            at android.view.View$PerformClick.run(View.java:18442)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5021)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
            at dalvik.system.NativeStart.main(Native Method)