Android:尝试获取null数组的长度

时间:2017-08-24 05:44:48

标签: java android nullpointerexception onclick bitmapfactory

我有一个包含两个表的SQLite数据库:主题表和Vocab表。我想点击图片按钮时显示词汇图像,但应用程序崩溃了。

Choice.java

    public class Choice extends AppCompatActivity {

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

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.pictureButton: {

        Intent intent = new Intent(Choice.this, Pictureandtext.class);
            startActivity(intent);
            break;
        }
        case R.id.textButton: {
            break;
        }
    }
}

}

我从

获得了我的意图
    this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            Intent intent = new Intent(Smode.this, Choice.class);
            intent.putExtra("SelectedTopicId", id);
            startActivity(intent);
        }
    });

这是 DatabaseAccess.java

中的getImage()方法
    public byte[] getImage(int i) {
    //byte[] data = null;
    String selectImage = "SELECT VocabImage FROM Vocab WHERE VocabTopic =" + i;
    Cursor cursor = database.rawQuery(selectImage, null);
    if (cursor == null) {
        cursor.moveToFirst();
        do {
            cursor.getBlob(0);

        } while (cursor.moveToNext());
    }
    cursor.close();
    return null;
}

Pictureandtext.java

    public class Pictureandtext extends AppCompatActivity {

DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);


protected Cursor cursor;
private ImageView imageView;
private TextView textView;
private int topicId;

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

    imageView = (ImageView)findViewById(R.id.imageView);
    textView = (TextView)findViewById(R.id.textView);

    topicId = getIntent().getIntExtra("SelectedTopicId", 0);

    databaseAccess.open();

    byte[] data = databaseAccess.getImage(topicId);
    Bitmap image = toBitmap(data);
    imageView.setImageBitmap(image);

    /*String name = databaseAccess.getVocabName(topicId);
    textView.setText(name);*/
    databaseAccess.close();
}

public static Bitmap toBitmap(byte[] image){
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

}

EDITED !! 我编辑了一些编码,我仍然得到一些错误。非常感谢您的帮助。谢谢。

致命的例外:主要                                                                               处理:com.example.user.displayvocab,PID:29339                                                                                java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.user.displayvocab / com.example.user.displayvocab.Pictureandtext}:java.lang.NullPointerException:尝试获取null数组的长度                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)                                                                                    在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                                                                 引起:java.lang.NullPointerException:尝试获取null数组的长度                                                                                    在com.example.user.displayvocab.Pictureandtext.toBitmap(Pictureandtext.java:47)                                                                                    在com.example.user.displayvocab.Pictureandtext.onCreate(Pictureandtext.java:38)                                                                                    在android.app.Activity.performCreate(Activity.java:6912)                                                                                    在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)                                                                                    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)                                                                                    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)                                                                                    在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                                                    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1635)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

1 个答案:

答案 0 :(得分:1)

  

致命异常:主程序:com.example.user.displayvocab,PID:   24087 java.lang.IllegalStateException:无法执行方法   android:onClick at

<强>问题

   case R.id.pictureButton: {
    setContentView(R.layout.pictureandtext); // Remove this line

............

 case R.id.textButton: {
            setContentView(R.layout.menu); // Remove this line
            break;
        }
  

setContentView-&gt;将活动内容设置为显式视图。这个   视图直接放在活动的视图层次结构中。

为什么多次拨打 setContentView ?删除此行。您可以在那里显示 DIALOG