设置Intent 和manifest无误 ,但 点击后就会 《已停止》
Main.java文件
package com.news_1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class Main extends Activity {
private ListView listView = null;
static String str[], strs;
String ttt;
Map<String, Object> map;
List<Map<String, Object>> data;
// List<Map<String, Object>> list;
Intent i1;
int x, i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressDialog myDialog = ProgressDialog.show(Main.this, "正在连接",
"please wait...", true, true);
Handler handler2 = new Handler();
new Thread() {
public void run() {
while (ttt == null) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
myDialog.dismiss();
}
}.start();
Runnable mTasks = new Runnable() {
public void run() {
Intent intent = new Intent();
intent.setClass(Main.this, Activity.class);
startActivity(intent);
}
};
data = new ArrayList<Map<String, Object>>();
new Thread() {
@Override
public void run() {
// String myJsonUrl =
// "http://wolfsayapi.sinaapp.com/news/api.php?act=index&type=1&page=1&size=10";
String myJsonUrl = "http://wolfsayapi.sinaapp.com/news/api.php?act=index&page=1&size=40";
HttpGet httpRequest = new HttpGet(myJsonUrl);
String strResult = "";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
strResult = EntityUtils.toString(httpResponse
.getEntity());
}
} catch (ClientProtocolException e) {
Log.i("DDD", "protocol error");
e.printStackTrace();
} catch (IOException e) {
Log.i("DDD", "IO error");
e.printStackTrace();
}
try {
JSONArray jsonObjs = new JSONObject(strResult)
.getJSONArray("data");
for (int i = 0; i < jsonObjs.length(); i++) {
JSONObject jsonObj = (JSONObject) jsonObjs.opt(i);
ttt = jsonObj.getString("title");
Log.i("DDD", "return: " + ttt);
map = new HashMap<String, Object>();
map.put("id", jsonObj.getString("id"));
map.put("img", jsonObj.getString("image"));
map.put("title", jsonObj.getString("title"));
map.put("time", jsonObj.getString("time"));
data.add(map);
}
// setData(data);
Message msg = new Message();
msg.what = 200;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 200) {
setData();
}
};
};
public void setData() {
listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new Myadapter(this, data));
listView.setOnItemClickListener(bln);
}
// public List<Map<String, Object>> getData() {
// data = new ArrayList<Map<String, Object>>();
// for (i = 1; i <= 16; i++) {
// map = new HashMap<String, Object>();
// map.put("img", R.drawable.wolfsay);
// map.put("title", ttt);
// map.put("time", "time");
// data.add(map);
// }
// return data;
// }
private ListView.OnItemClickListener bln = new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Log.i("data->",""+data.get(arg2).get("title"));
x = (Integer) map.get(arg1);
i1 = new Intent(Main.this, NewsPage.class);
Bundle bundle = new Bundle();
bundle.putString("title", (String) data.get(arg2).get("title"));
bundle.putInt("id", x);
i1.putExtras(bundle);
startActivity(i1);
return;
}
};
}
NewsPage.java文件
package com.news_1;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
public class NewsPage extends Activity
{
TextView t1,t2;
String url="http://wolfsayapi.sinaapp.com/news/api.php?act=content&id=";
String result,text;
HttpResponse httpResponse2=null;
Bundle bundle = this.getIntent().getExtras();
String title = bundle.getString("title");
int id=bundle.getInt("id");
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newspage);
new Thread() {
public void run()
{
HttpGet httpRequest2 = new HttpGet(url+id);
String strResult2 = "";
try {
HttpClient httpClient2 = new DefaultHttpClient();
HttpResponse httpResponse2 = httpClient2.execute(httpRequest2);
if (httpResponse2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
strResult2 = EntityUtils.toString(httpResponse2
.getEntity());
}
} catch (ClientProtocolException e) {
Log.i("AAA", "protocol error");
e.printStackTrace();
} catch (IOException e) {
Log.i("AAA", "IO error");
e.printStackTrace();
}
try {
text=new JSONObject(strResult2).getString("content");
Log.i("AAA", "return: " + text);
// setData(data);
Message msg2 = new Message();
msg2.what=200;
handler3.sendMessage(msg2);
} catch (JSONException e) {
e.printStackTrace();
}
}
}.start();
}
@SuppressLint("HandlerLeak")
private Handler handler3 =new Handler(){
public void handleMessage(android.os.Message msg2)
{
if(msg2.what==200)
{
setLayout();
}
};
};
public void setLayout()
{
t1=(TextView)this.findViewById(R.id.title);
t1.setText(title);
t2=(TextView)this.findViewById(R.id.content);
t2.setText(text);
}
}
Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
package="com.news_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name="com.news_1.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.news_1.NewsPage"
android:label="@string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
我不知道是什么错误,只要点击 进行跳转 就会 停止,Intent Manifest文件 网络权限都没问题 这个问题折磨了我很久
望大师指教