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


Fatal exception main : Cannot start activity component info

我正在尝试将我的安卓连接到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
        }
    }

这是我的清单.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文件中添加了文本视图,确保在调用findViewById()之前调用setcontentview()。而且我没有删除所有自动生成的方法。请告诉我问题是什么。

Android 不建议在主线程上调用网络调用,

这就是为什么您收到错误而不是在主线程上使用异步任务调用网络调用,或者您想在主线程上调用只需在 setContentView() 之后的 onCreate() 中添加以下行

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);