Java 致命异常:无法启动活动组件信息

Java 致命异常:无法启动活动组件信息,java,php,android,Java,Php,Android,我正在尝试将我的android连接到php。我在网上找到了一些代码,但它总是给我这个例外。这是我的主要活动 public class MainActivity extends ActionBarActivity { TextView tv; String text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

我正在尝试将我的android连接到php。我在网上找到了一些代码,但它总是给我这个例外。这是我的主要活动

public class MainActivity extends ActionBarActivity {
    TextView tv;
    String text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv  = (TextView)findViewById(R.id.textview);
        text    = "";

        try {
            postData();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void postData() throws JSONException{
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.99.7.91/post.php");
        JSONObject json = new JSONObject();

        try {
            // JSON data:
            json.put("name", "puffles");
            json.put("position", "lame");

            JSONArray postjson=new JSONArray();
            postjson.put(json);

            // Post the data:
            httppost.setHeader("json",json.toString());
            httppost.getParams().setParameter("jsonpost",postjson);

            // Execute HTTP Post Request
            System.out.print(json);
            HttpResponse response = httpclient.execute(httppost);

            // for JSON:
            if(response != null)
            {
                InputStream is = response.getEntity().getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                text = sb.toString();
            }

            tv.setText(text);

        }catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
这是我的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fatima.scmyproject" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

</manifest>

这里是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/scrollView1">
        <LinearLayout android:layout_width="match_parent"
            android:id="@+id/linearLayout1"
            android:layout_height="match_parent">
            <TextView android:id="@+id/textview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="hellow" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>


到目前为止,我没有发现我的代码有任何问题。我在xml文件中添加了textview,确保在调用findViewById()之前调用setcontentview()。我还没有删除所有自动生成的方法。请告诉我是什么问题

Android不建议在主线程上调用network call,这就是为什么在主线程上调用network call会出错,而不是在主线程上调用use Async任务,或者您想在主线程上调用,只需在setContentView()之后的activity的onCreate()中添加以下行


已经询问了100次不要在主线程上执行联网操作。在
AsyncTask
中运行代码。阅读本文,了解什么是
AsyncTask
,以及如何实现它:向下投票。请不要建议任何人(特别是初学者(这是初学者的错误))更改ThreadPolicy。这不会解决问题,这只会推迟问题。
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);