android - 当我在 android studio 中运行我的程序时出现错误 "unfortunately, my application has stopped"

标签 android android-studio

在 android studio 中运行我的程序时出现错误。这是以下消息错误:

10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime: Process: com.example.natasya.myapplication, PID: 3478
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.natasya.myapplication/com.example.natasya.myapplication.MainActivity}: java.lang.IllegalArgumentException: invalid provider: null
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: invalid provider: null
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.location.LocationManager.checkProvider(LocationManager.java:1704)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1194)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.example.natasya.myapplication.MainActivity.getlongilatifile(MainActivity.java:103)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.example.natasya.myapplication.MainActivity.onCreate(MainActivity.java:76)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

MainActivity.java第103行如下:

Location location = locationManager.getLastKnownLocation(bestProvider);

这是 AndroidManifest.xml 中的权限代码:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" ></uses-permission>

我很困惑为什么会这样?请帮助我。

这是主 Activity :

import java.io.File;
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.text.SimpleDateFormat;
import java.util.List;

public class MainActivity extends Activity {

public double longi1 = 0.0;
public double lati1 = 0.0;
public LocationListener locationListener ;
public TextView Longi1;
public TextView Lati1;

Button b1;
int cekbutton;
public String filename="";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date now = new Date();
String fileName = formatter.format(now);
private static final String AUDIO_RECORDER_FILE_EXT_AMR = ".amr";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";

private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = {MediaRecorder.OutputFormat.RAW_AMR};
private String file_exts[] = {AUDIO_RECORDER_FILE_EXT_AMR};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(Build.VERSION.SDK_INT > 9){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    b1 = (Button) findViewById(R.id.button);
    getlongilatifile();
    b1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if(cekbutton==0)
            {
                cekbutton=1;
                b1.setText("STOP");
                startRecording();
            }
            else if(cekbutton==1)
            {
                stopRecording();
                Intent i = new Intent(MainActivity.this,halaman2.class);
                i.putExtra("nama", filename);
                startActivity(i);

            }

        }
    });

}

public void getlongilatifile(){
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(bestProvider);

    Longi1= (TextView) findViewById(R.id.textView1);
    Lati1= (TextView) findViewById(R.id.textView);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub

            longi1 = location.getLongitude();
            lati1 = location.getLatitude();
            Longi1.setText(longi1+"");
            Lati1.setText(lati1+"");

            try {
                DefaultHttpClient hc=new DefaultHttpClient();
                ResponseHandler<String> res=new BasicResponseHandler();
                HttpPost postMethod = new HttpPost("http://10.0.2.2:8080/send_malem_3.php");
                System.out.printf("test setelah nyambung");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("nama", fileName+".amr"));
                postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response2 = hc.execute(postMethod);
                } 
catch (ClientProtocolException e) 
{
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getBaseContext(), "Gagal 2", Toast.LENGTH_SHORT).show();
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

}

private void startRecording(){
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    getFilename();
    recorder.setOutputFile(filename);

    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void getFilename(){
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);

    if(!file.exists()){
        file.mkdirs();
    }
    filename = (file.getAbsolutePath() + "/" + fileName + file_exts[currentFormat]);
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        AppLog.logString("Error: " + what + ", " + extra);
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        AppLog.logString("Warning: " + what + ", " + extra);
    }
};
private void stopRecording(){
    if(null != recorder){
        recorder.stop();
        recorder.reset();
        recorder.release();

        recorder = null;
    }
}

谢谢, 阿贡

最佳答案

传递给 getLastKnownLocation 的提供者应该是以下字符串常量之一:

这是 source code of getLastKnownLocation :

public Location getLastKnownLocation(String provider) {
    checkProvider(provider);
    String packageName = mContext.getPackageName();
    LocationRequest request = LocationRequest.createFromDeprecatedProvider(
            provider, 0, 0, true);
    try {
        return mService.getLastLocation(request, packageName);
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException", e);
        return null;
    }
}

和方法checkProvider:

private static void checkProvider(String provider) {
    if (provider == null) {
        throw new IllegalArgumentException("invalid provider: " + provider);
    }
}

由此我们可以得出结论,您的参数 bestProvider 为空,而不是有效的提供程序字符串。

关于android - 当我在 android studio 中运行我的程序时出现错误 "unfortunately, my application has stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281335/

相关文章:

android - 通过自己的IP通过RTP进行Android实时音频流传输

android - Android Studio 2.2.1 Gradle同步NullPointerException

android - 意外的顶级异常android studio

android - "The following classes could not be found"在布局中使用自定义 Kotlin View

android - Apk 在 Google Play 中卡住了 "in prod"

android - 使用 git 修改 android 源代码的正确方法

java - Android:为每个设备自动调整大小

java - 无法打开某些 Activity java 文件

java - 禁用 Dart 和 Kotlin 插件后 Android Studio 未运行

java - 对 codenameone 的操作,构建错误(第 2 部分)