-1

This is my code in java:

public class TestAudio extends Activity {
    private TextView t1;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_audio);
        t1 = (TextView)findViewById(R.id.t1);
        System.out.println("aaaaaaaaaaaaaa");
        PackageManager packageManager = context.getPackageManager();

        System.out.println("bbbbbbbbbbbbbb");
        if(packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)){
            t1.setText("I'm here");
        }else{
            t1.setText("gg");
        }

    }

Why does it stop response when it executes to the code between the two system.out.print?

AL.
  • 36,815
  • 10
  • 142
  • 281
MarvinC
  • 89
  • 1
  • 1
  • 8

3 Answers3

1

I'm presuming that the error your getting is NPE. Since your context has not been initialized yet. Initialize it first.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • Thank you! Now I know what the problem is, I cannot just instantiate a Context object – MarvinC May 26 '16 at 16:19
  • Do properly tag your post as answered by accepting the answer that's helped you @MarvinC Cheers! – AL. May 26 '16 at 23:25
1

You have not initialized your context. To get the package manager you can use your activity's context. Get the activity's context to initialize package manager:

PackageManager packageManager = this.getPackageManager();

Or simply

PackageManager packageManager = getPackageManager();
0

Init your context before use it:

this.context = getApplicationContext();
Duc Nguyen
  • 87
  • 9