java - 无效响应

标签 java android

我正在尝试一个示例链接 http://www.semurjengkol.com/populating-android-listview-with-json-based-data-fetched-from-mysql-server-using-php/,i尝试了同样的事情,但我无法得到解决方案

activity_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

Mainactivity.java

public class MainActivity extends ListActivity implements FetchDataListener{
    private ProgressDialog dialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);       
            //setContentView(R.layout.activity_list_item);   
            initView();   
        }

        private void initView() {
            // show progress dialog
            dialog = ProgressDialog.show(this, "", "Loading...");

            String url = "http://10.0.2.2/test/apps.php";
            FetchDataTask task = new FetchDataTask(this);
            task.execute(url);
        }

        @Override
        public void onFetchComplete(List<Application> data) {
            // dismiss the progress dialog
            if(dialog != null)  dialog.dismiss();
            // create new adapter
            ApplicationAdapter adapter = new ApplicationAdapter(this, data);
            // set the adapter to list
            setListAdapter(adapter);        
        }

        @Override
        public void onFetchFailure(String msg) {
            // dismiss the progress dialog
            if(dialog != null)  dialog.dismiss();
            // show failure message
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
        }
    }

我将在这里提到的主要问题..

如果我注释掉 setcontent View 不显示任何错误,我也可以运行,但在模拟器中它显示错误 无效响应

如果我没有注释掉链接logcat会显示这样的错误

内容必须有一个 id 属性为“android.R.id.list”的 ListView

Logcat 错误:

5-17 11:30:30.649: E/AndroidRuntime(1177): FATAL EXCEPTION: main
05-17 11:30:30.649: E/AndroidRuntime(1177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonandroid/com.example.jsonandroid.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.os.Looper.loop(Looper.java:137)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at dalvik.system.NativeStart.main(Native Method)
05-17 11:30:30.649: E/AndroidRuntime(1177): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.Activity.setContentView(Activity.java:1881)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at com.example.jsonandroid.MainActivity.onCreate(MainActivity.java:17)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.Activity.performCreate(Activity.java:5104)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-17 11:30:30.649: E/AndroidRuntime(1177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-17 11:30:30.649: E/AndroidRuntime(1177):     ... 11 more

这可能是什么解决方案,这是示例中的问题还是 URL 问题

最佳答案

URL http://10.0.2.2/test/apps.php 表示您的本地主机。您必须拥有 php 文件并运行 php 服务器才能成功执行客户端程序。

关于java - 无效响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609113/

相关文章:

java - 配置cygwin构建ndk项目

java - 方向改变时构造函数会被调用吗?

Java OO MVC 问题

Androidx 首选项库与数据存储首选项

android - 如何在设备启动时使用 AlarmManager 启动警报?

Java 找不到符号 get.Method 但声明了 Method

java - 如何在 Google 自定义搜索中指定图像类型

java - 使用构建器模式最多设置一次值

java - 无法找到或加载主类 sec java

android - 测试rxjava回调函数