1
我有一個問題。我需要傳遞一個字符串到另一個Android意圖,我得到這個錯誤。我已經完成了這個通過。不知道爲什麼它現在不工作。這是我得到的。傳遞字符串在另一個意圖不工作
> 10-29 15:17:03.254: E/AndroidRuntime(1641): FATAL EXCEPTION: main
> 10-29 15:17:03.254: E/AndroidRuntime(1641):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.projectx/com.example.projectx.InfoActivity}:
> java.lang.NullPointerException 10-29 15:17:03.254:
> E/AndroidRuntime(1641): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.ActivityThread.access$600(ActivityThread.java:141) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> android.os.Handler.dispatchMessage(Handler.java:99) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> android.os.Looper.loop(Looper.java:137) 10-29 15:17:03.254:
> E/AndroidRuntime(1641): at
> android.app.ActivityThread.main(ActivityThread.java:5103) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> java.lang.reflect.Method.invokeNative(Native Method) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> java.lang.reflect.Method.invoke(Method.java:525) 10-29 15:17:03.254:
> E/AndroidRuntime(1641): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> dalvik.system.NativeStart.main(Native Method) 10-29 15:17:03.254:
> E/AndroidRuntime(1641): Caused by: java.lang.NullPointerException
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> com.example.projectx.InfoActivity.onCreate(InfoActivity.java:31) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.Activity.performCreate(Activity.java:5133) 10-29
> 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
> 10-29 15:17:03.254: E/AndroidRuntime(1641): ... 11 more 10-29
> 15:17:08.494: I/Process(1641): Sending signal. PID: 1641 SIG: 9
這些都是我的兩個活動課
主要活動:
package com.example.projectx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
private LinearLayout mainlayout;
private LinearLayout list_of_countries;
private ScrollView scroll;
private EditText search;
private View countryline;
private Button searchButton;
private ArrayList<JSONObject> countriesJSON;
private HashMap<String,JSONObject> countriesINFO;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
scroll=new ScrollView(this);
mainlayout=new LinearLayout(this);
list_of_countries =new LinearLayout(this);
search=new EditText(this);
searchButton=new Button(this);
countriesINFO=new HashMap<String, JSONObject>();
String jsoncountries=readData("http://api.worldbank.org/countries?per_page=256&format=json");
countriesJSON=getCountriesJSON(jsoncountries);
CreateGUI();
setContentView(mainlayout);
}
public void CreateGUI()
{
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
mainlayout.setOrientation(LinearLayout.VERTICAL);
mainlayout.setLayoutParams(params);
search.setHint("Search...");
mainlayout.addView(search);
LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
list_of_countries.setOrientation(LinearLayout.VERTICAL);
list_of_countries.setLayoutParams(params2);
for(int i=0; i<countriesJSON.size();i++)
{
try {
if (countriesJSON.get(i).getString("capitalCity").equals("")) { }
else {
name = countriesJSON.get(i).getString("name");
countriesINFO.put(name, countriesJSON.get(i));
countryline= getLayoutInflater().inflate(R.layout.country_line, list_of_countries, false);
list_of_countries.addView(countryline);
TextView country= (TextView)countryline.findViewById(R.id.textView1);
country.setTextSize(26.0f);
country.setTextColor(Color.WHITE);
country.setText(name);
country.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//intent.putExtra("data", countriesINFO);
startIntent();
}
});
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// search.setText(countriesINFO.size()+"");
scroll.addView(list_of_countries);
mainlayout.addView(scroll);
}
@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;
}
/*A method which starts the InfoActivity and put the HashMap and the name of the country*/
public void startIntent() {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.putExtra("name", name);
startActivity(intent);
}
/*END*/
/**A METHOD TO GET ALL JSON OBJECTS OF ALL COUNTRIES*/
public ArrayList<JSONObject> getCountriesJSON(String json) {
ArrayList<JSONObject> countriesJSON=new ArrayList<JSONObject>();
try {
JSONArray array=new JSONArray(json);
JSONArray countries=array.getJSONArray(1);
for (int i=0;i<countries.length();i++) {
countriesJSON.add(countries.getJSONObject(i));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return countriesJSON;
}
/*END OF THAT METHOD*/
/**READ DATA METHOD*/
public String readData(String url) {
// Create download objects
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
StringBuilder content = new StringBuilder();
try {
// Execute response and create input stream
HttpResponse response = client.execute(get);
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 200) {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// Build string from input stream
String readLine = reader.readLine();
while (readLine != null) {
content.append(readLine);
readLine = reader.readLine();
}
} else {
Log.w("DATA RETRIEVAL","Unable to read data. HTTP response code = " + responseCode);
content = null;
}
} catch (ClientProtocolException e) {
Log.e("readData","ClientProtocolException:\n"+e.getMessage());
} catch (IOException e) {
Log.e("readData","IOException:\n+e.getMessage()");
}
// return data
if (content==null) {
return(null);
} else {
return(content.toString());
}
}
/*END OF READ DATA METHOD*/
}
信息活動:
package com.example.projectx;
import java.io.Serializable;
import java.util.HashMap;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class InfoActivity extends Activity {
private TextView country;
private HashMap<String,JSONObject> countriesINFO;
private String countryNAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
country=(TextView) findViewById(R.id.text);
//countryNAME=intent.getStringExtra("name");
/*if (intent.getExtras().size()==1) {
/*countriesINFO= (HashMap<String, JSONObject>) intent.getSerializableExtra("data");*/
/*country.setText("evala"); } */
Bundle extras = getIntent().getExtras();
countryNAME = extras.getString("name");
country.setText(extras.size());
setContentView(R.layout.activity_info);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.info, menu);
return true;
}
}
你能刪除那些>並重新格式化輸出嗎? – njzk2
'setContentView'前面'findViewById'的典型情況 – njzk2