“遗憾的是zilsesiayaryapma已停止”涉及错误 “设为铃声”(buton1)你无法设置铃声点击 让别人告诉我该怎么做
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
Button buton1;
MediaPlayer mp;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this ;
String name = "american_crow.mp3";
File file = new File(Environment.getExternalStorageDirectory(),
"/myRingtonFolder/Audio/");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myRingtonFolder/Audio/";
File f = new File(path + "/", name + ".mp3");
Uri mUri = Uri.parse("android.resource://"
+ context.getPackageName() + "/raw/" + name);
ContentResolver mCr = context.getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile = mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile = null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(f);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, name);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, f.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
.getAbsolutePath());
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_RINGTONE, newUri);
Settings.System.putString(mCr, Settings.System.RINGTONE,
newUri.toString());
} catch (Throwable t) {
Toast.makeText(MainActivity.this, "American Crow",
Toast.LENGTH_SHORT).show();
}
buton1 = (Button) findViewById(R.id.button1);
buton1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this,
R.raw.american_crow);
mp.start();
Toast.makeText(MainActivity.this, "American Crow",
Toast.LENGTH_SHORT).show();
}
}
日志信息如下;
05-27 20:26:14.866: W/ApplicationPackageManager(16084): getCSCPackageItemText()
05-27 20:26:14.911: D/AndroidRuntime(16084): Shutting down VM
05-27 20:26:14.911: W/dalvikvm(16084): threadid=1: thread exiting with uncaught exception (group=0x41876c08)
05-27 20:26:14.916: E/AndroidRuntime(16084): FATAL EXCEPTION: main
05-27 20:26:14.916: E/AndroidRuntime(16084): Process: com.example.zilsesiayaryapma, PID: 16084
05-27 20:26:14.916: E/AndroidRuntime(16084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zilsesiayaryapma/com.example.zilsesiayaryapma.MainActivity}: java.lang.NullPointerException
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.os.Handler.dispatchMessage(Handler.java:102)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.os.Looper.loop(Looper.java:157)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-27 20:26:14.916: E/AndroidRuntime(16084): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 20:26:14.916: E/AndroidRuntime(16084): at java.lang.reflect.Method.invoke(Method.java:515)
05-27 20:26:14.916: E/AndroidRuntime(16084): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-27 20:26:14.916: E/AndroidRuntime(16084): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-27 20:26:14.916: E/AndroidRuntime(16084): at dalvik.system.NativeStart.main(Native Method)
05-27 20:26:14.916: E/AndroidRuntime(16084): Caused by: java.lang.NullPointerException
05-27 20:26:14.916: E/AndroidRuntime(16084): at com.example.zilsesiayaryapma.MainActivity.onCreate(MainActivity.java:62)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.Activity.performCreate(Activity.java:5426)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-27 20:26:14.916: E/AndroidRuntime(16084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
05-27 20:26:14.916: E/AndroidRuntime(16084): ... 11 more
帮帮我......
答案 0 :(得分:1)
您永远不会初始化上下文
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context =this;