我的logcat:
01-01 05:13:28.690: D/AndroidRuntime(696): Shutting down VM
01-01 05:13:28.690: W/dalvikvm(696): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-01 05:13:28.741: E/AndroidRuntime(696): FATAL EXCEPTION: main
01-01 05:13:28.741: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfunapp/com.example.myfunapp.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.os.Looper.loop(Looper.java:137)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-01 05:13:28.741: E/AndroidRuntime(696): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 05:13:28.741: E/AndroidRuntime(696): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 05:13:28.741: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-01 05:13:28.741: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-01 05:13:28.741: E/AndroidRuntime(696): at dalvik.system.NativeStart.main(Native Method)
01-01 05:13:28.741: E/AndroidRuntime(696): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-01 05:13:28.741: E/AndroidRuntime(696): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.Activity.setContentView(Activity.java:1867)
01-01 05:13:28.741: E/AndroidRuntime(696): at com.example.myfunapp.MainActivity.onCreate(MainActivity.java:39)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.Activity.performCreate(Activity.java:5008)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-01 05:13:28.741: E/AndroidRuntime(696): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-01 05:13:28.741: E/AndroidRuntime(696): ... 11 more
01-01 05:13:33.503: I/Process(696): Sending signal. PID: 696 SIG: 9
它说“遗憾的是myapp已关闭”,我确实在xml文件的ListView中包含了@android:id / list。但它在启动模拟器时仍然给出了同样的错误。请帮忙,对此很新..
这是我的MainActivity.java类:
public class MainActivity extends ListActivity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fun_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.takePhoto:
cam();
return true;
case R.id.music:
startActivity(new Intent(this, MPlayer.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void cam() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"my-photo.jpg");
Uri photoPath = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
startActivityForResult(intent, 0);
}
这是用于实现音乐播放器的MPlayer.java类。我在MainActivity中使用了switch case。一个用于音乐播放器,另一个用于android中的相机API。 我直接调用了一个方法,我在MainActivity中为Camera选项实现了该方法。但是对于音乐播放器,我创建了一个名为MPlayer.java的新类,其中包含代码。
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import com.example.myfunapp.Mp3Filter;
import com.example.myfunapp.R;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
class Mp3Filter implements FilenameFilter{
public boolean accept(File dir, String name){
return(name.endsWith(".mp3"));
}
public static class MPlayer extends ListActivity{
private static final String SD_PATH =new String("/sdcard/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msicplayer);
updatePlayList();
Button stopPlay = (Button) findViewById(R.id.stopBtn);
stopPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp.stop();
}
});
}
private void updatePlayList(){
File home = new File(SD_PATH);
if (home.listFiles(new Mp3Filter()).length > 0){
for (File file : home.listFiles(new Mp3Filter())){
songs.add(file.getName());
}
ArrayAdapter<String> songList =new ArrayAdapter<String>(this,R.layout.song_item,songs);
setListAdapter(songList);
}
}
} }
这是我的音乐播放器布局代码:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stopBtn" />
</LinearLayout>
答案 0 :(得分:0)
如错误日志所示,Android无法找到具有正确ID的视图。这可能是因为您需要将其添加为
android:id="@+id/list"
答案 1 :(得分:0)
检查R类的导入包。
有两个R类,一个是android系统,另一个是你的
确保通过[your_package_name] .R