Я не могу использовать метод getAssets без MainActivity


Когда я писал код в Android Studio, я не мог использовать getAssets без MainActivity.
Здесь MainActivity.

package com.example.maguro.mnist_bg2;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

public class MainActivity extends AppCompatActivity {
    CanvasView canvasview; //already exist CanvasView class
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        canvasview = new CanvasView(this);
        setContentView(canvasview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.item1:
                canvasview.onReset();
                break;
            case R.id.item2:
                canvasview.onDetect();
                break;
            case R.id.item3:
                finish();
                break;
        }
        return true;
    }

}

А вот еще один пример класса.

package com.example.maguro.mnist_bg2;

import android.content.Context;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;


public class DigitDetector {
    static{
        System.loadLibrary("tensorflow_inference");
    }

    protected void onDetect(byte[] bytes){
        TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(getAssets(), "beginner-graph.pb");
    }

}

Точка ошибки - почти последняя строка TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(getAssets(), "beginner-graph.pb");Хотя Android Studio сказал

Не удается разрешить метод 'getAssets ()

Android Studio не сказала эту ошибку в MainActivity

Даже если я приравниваю другой модуль класса с MainActivityAndroid Studio сказала ту же ошибку.

Почему произошла ошибка?

Пожалуйста, одолжите мне свои руки.

2 ответа

Решение
import android.content.Context;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;


public class DigitDetector {

Context context;

public DigitDetector(Context context) {
    this.context =context;
}

static{
    System.loadLibrary("tensorflow_inference");
}

protected void onDetect(byte[] bytes){
    TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(context.getAssets(), "beginner-graph.pb");
}

}

Попробуйте создать конструктор и передать на него контекст активности. и использовать его для метода getAssets().

Создайте конструктор в вашем классе. например:-

Context context;
DigitDetector(Context ctx){
context = ctx;
}

и использовать его как.

context.getAssets()....
Другие вопросы по тегам