我在这个 Android 应用程序中遇到问题。当我按下 button_x 时,它总是崩溃。该程序如下所列。我使用 BufferedReader 从 Internet 读取内容。
public class MainActivity extends Activity {
Button button_x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_x = (Button)findViewById(R.id.button_x);
button_x.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
URL yahoo = null;
try {
yahoo = new URL("http://www.google.com.tw/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String inputLine;
try {
while ((inputLine = in.readLine()) != null)
button_x.setBackgroundColor(Color.RED);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我以为是上网权限引起的,所以我在manifest文件上加上了上网权限。
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.euro.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>
</manifest>
谁能告诉我为什么这个程序会崩溃?