-2

I have a SQLite database with two tables: Topic table and Vocab table. I want to display the vocab images when i click on the pictureButton, but the app crashes.

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;
        }
    }
}

}

I get my intent from

    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);
        }
    });

This is my getImage() method from the DatabaseAccess.java

    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!! I edited some coding and im still getting some error. Your help is much appreciated. Thank you.

FATAL EXCEPTION: main Process: com.example.user.displayvocab, PID: 29339 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.displayvocab/com.example.user.displayvocab.Pictureandtext}: java.lang.NullPointerException: Attempt to get length of null array at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Caused by: java.lang.NullPointerException: Attempt to get length of null array at com.example.user.displayvocab.Pictureandtext.toBitmap(Pictureandtext.java:47) at com.example.user.displayvocab.Pictureandtext.onCreate(Pictureandtext.java:38) at android.app.Activity.performCreate(Activity.java:6912) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)  at android.app.ActivityThread.-wrap14(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6692)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

chernting
  • 61
  • 2
  • 8

1 Answers1

1

FATAL EXCEPTION: main Process: com.example.user.displayvocab, PID: 24087 java.lang.IllegalStateException: Could not execute method for android:onClick at

Problem

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

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

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

setContentView->Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

Why you calling setContentView multiple-time ? Remove this line .You can show DIALOG there .

IntelliJ Amiya
  • 70,230
  • 14
  • 154
  • 181
  • I removed them and created a new class so when I onclick pictureButton it should bring to the new class. But the app still crashes and no error is shown this time. Could it be something wrong with my getImage method? – chernting Aug 24 '17 at 06:46