我无法实例化活动组件信息:mainactivity:NullPointerException 我的第一个屏幕是一个启动屏幕,它将数据传递给主要活动。主要活动将数据传递给它的片段。此数据用于加载微调器下拉列表。互联网访问是在片段中完成的,之前的代码正在运行。因此,当每次选择片段时,从服务器检索数据以加载下拉列表。但现在我只是使用异步任务在初始启动屏幕中从服务器获取数据,并通过意图将其传递给主要活动。使用getDepartment()等函数,片段可以访问字符串。
FragmentTab1.java
@Override
public SherlockFragmentActivity getSherlockActivity() {
return super.getSherlockActivity();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
final Spinner spinner1 = (Spinner) view.findViewById(R.id.Spinner1);
// Spinner click listener
spinner1.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("select");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//MainActivity x=new MainActivity();
try {
// dept_response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android_db/ppf4_dept.php",response_Parameters);
//dept_response = CustomHttpClient.executeHttpPost("http://webdev1.tifr.res.in/~PFproj/pplf/ppf4_dept.php",response_Parameters,FragmentTab1.this.getActivity());
String dept_result=((MainActivity)this.getActivity()).getDepartment();
JSONArray jArray = new JSONArray(dept_result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
//categories.add(json_data.getString("dept"));}
categories.add(json_data.getString("PF_dept"));}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
final Spinner spinner2 = (Spinner) view.findViewById(R.id.Spinner2);
// Spinner click listener
spinner2.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories2 = new ArrayList<String>();
categories2.add("select");
// ArrayList<NameValuePair> response_Parameters = new ArrayList<NameValuePair>();
try {
//role_response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android_db/ppf4_role.php",response_Parameters);
//role_response = CustomHttpClient.executeHttpPost("http://webdev1.tifr.res.in/~PFproj/pplf/ppf4_role.php",response_Parameters,FragmentTab1.this.getActivity());
String role_result=((MainActivity)this.getActivity()).getRole();
JSONArray jArray = new JSONArray(role_result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
categories2.add(json_data.getString("role"));}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, categories2);
// Drop down layout style - list view with radio button
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner2.setAdapter(dataAdapter2);
//spinner3
final Spinner spinner3 = (Spinner) view.findViewById(R.id.Spinner3);
// Spinner click listener
spinner3.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories3 = new ArrayList<String>();
categories3.add("select");
String desig_response = null;
try {
//desig_response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android_db/ppf4_desig.php",response_Parameters);
//desig_response = CustomHttpClient.executeHttpPost("http://webdev1.tifr.res.in/~PFproj/pplf/ppf4_desig.php",response_Parameters,FragmentTab1.this.getActivity());
String desig_result=((MainActivity)this.getActivity()).getDesignation();
JSONArray jArray = new JSONArray(desig_result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
// String name=json_data.getString("dept");
categories3.add(json_data.getString("designation"));}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, categories3);
// Drop down layout style - list view with radio button
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner3.setAdapter(dataAdapter3);
MainActivity
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener, SearchView.OnSuggestionListener {
//***********************************************************************************************************
// start of actionbarsherlock tabnavigation
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;
DBController controller = new DBController(this);
Intent i = getIntent();
String dept_response = i.getExtras().getString("dept_response");
String role_response = i.getExtras().getString("role_response");
String desig_response = i.getExtras().getString("desig_response");
public String getDepartment(){
return this.dept_response;
}
public String getRole(){
return this.role_response;
}
public String getDesignation(){
return this.desig_response;
}
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
@Override
protected void onCreate(Bundle savedInstanceState) {
logcat的:
01-28 22:24:09.623: D/AndroidRuntime(1123): Shutting down VM
01-28 22:24:09.623: W/dalvikvm(1123): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-28 22:24:09.663: E/AndroidRuntime(1123): FATAL EXCEPTION: main
01-28 22:24:09.663: E/AndroidRuntime(1123): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tab/com.example.tab.MainActivity}: java.lang.NullPointerException
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.os.Looper.loop(Looper.java:123)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-28 22:24:09.663: E/AndroidRuntime(1123): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 22:24:09.663: E/AndroidRuntime(1123): at java.lang.reflect.Method.invoke(Method.java:507)
01-28 22:24:09.663: E/AndroidRuntime(1123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-28 22:24:09.663: E/AndroidRuntime(1123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-28 22:24:09.663: E/AndroidRuntime(1123): at dalvik.system.NativeStart.main(Native Method)
01-28 22:24:09.663: E/AndroidRuntime(1123): Caused by: java.lang.NullPointerException
01-28 22:24:09.663: E/AndroidRuntime(1123): at com.example.tab.MainActivity.<init>(MainActivity.java:49)
01-28 22:24:09.663: E/AndroidRuntime(1123): at java.lang.Class.newInstanceImpl(Native Method)
01-28 22:24:09.663: E/AndroidRuntime(1123): at java.lang.Class.newInstance(Class.java:1409)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-28 22:24:09.663: E/AndroidRuntime(1123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-28 22:24:09.663: E/AndroidRuntime(1123): ... 11 more
01-28 22:24:15.943: I/Process(1123): Sending signal. PID: 1123 SIG: 9
答案 0 :(得分:1)
Intent i = getIntent();
返回null。在调用Activity
之前,您无法使用onCreate
的任何方法,因为尚未设置信息。
移动
DBController controller = new DBController(this);
Intent i = getIntent();
String dept_response = i.getExtras().getString("dept_response");
String role_response = i.getExtras().getString("role_response");
String desig_response = i.getExtras().getString("desig_response");
进入onCreate
,因为它无法正常工作。