这是我得到的错误:
public class MainActivity_d2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
//Declaring an Spinner
private Spinner spinner2, spinner1;
//An ArrayList for Spinner Items
private ArrayList<String> students1;
private ArrayList<String> students2;
//JSON Array
private JSONArray result1, result2, result;
//TextViews to display details
private TextView textViewName1;
private TextView textViewName2;
private TextView textViewCourse;
private TextView textViewSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity_d1);
//Initializing the ArrayList
students1 = new ArrayList<String>();
students2 = new ArrayList<String>();
//Initializing Spinner
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
// spinner1.setOnItemSelectedListener(this);
//Initializing TextViews
textViewName1 = (TextView) findViewById(R.id.textViewName1);
textViewName2 = (TextView) findViewById(R.id.textViewName2);
// textViewCourse = (TextView) findViewById(R.id.textViewCourse);
// textViewSession = (TextView) findViewById(R.id.textViewSession);
//This method will fetch the data from the URL
getData1();
getData2();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
/* switch (view.getId()){
case R.id.spinner1:
getData1();
break;
case R.id.spinner2:
getData2();
break;
}*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
if (spinner1.getId() == R.id.spinner1) {
//do this
textViewName1.setText("");
} else if (spinner2.getId() == R.id.spinner2) {
//do this
textViewName2.setText("");
}
}
private void getData1() {
//Creating a string request
StringRequest stringRequest1 = new StringRequest(Config.DATA_URL1,
new Response.Listener<String>() {
@Override
public void onResponse(String response1) {
JSONObject j1 = null;
try {
//Parsing the fetched Json String to JSON Object
j1 = new JSONObject(response1);
//Storing the Array of JSON String to our JSON Array
result1 = j1.getJSONArray(Config.JSON_ARRAY1);
//Calling method getStudents to get the students from the JSON Array
getStudents1(result1);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error1) {
}
});
//Creating a request queue
RequestQueue requestQueue1 = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue1.add(stringRequest1);
}
private void getStudents1(JSONArray j1) {
//Traversing through all the items in the json array
for (int i = 0; i < j1.length(); i++) {
try {
//Getting json object
JSONObject json1 = j1.getJSONObject(i);
//Adding the name of the student to array list
students1.add(json1.getString(Config.TAG_COURSE));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner1.setAdapter(new ArrayAdapter<String>(MainActivity_d2.this, android.R.layout.simple_spinner_dropdown_item, students1));
}
//Initializing TextViews
// textViewCourse = (TextView) findViewById(R.id.textViewCourse);
// textViewSession = (TextView) findViewById(R.id.textViewSession);
//This method will fetch the data from the URL
private void getData2() {
//Creating a string request
StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2,
new Response.Listener<String>() {
@Override
public void onResponse(String response2) {
JSONObject j2 = null;
try {
//Parsing the fetched Json String to JSON Object
j2 = new JSONObject(response2);
//Storing the Array of JSON String to our JSON Array
result = j2.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents2(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error1) {
}
});
//Creating a request queue
RequestQueue requestQueue2 = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue2.add(stringRequest2);
}
private void getStudents2(JSONArray j2) {
//Traversing through all the items in the json array
for (int i = 0; i < j2.length(); i++) {
try {
//Getting json object
JSONObject json2 = j2.getJSONObject(i);
//Adding the name of the student to array list
students2.add(json2.getString(Config.TAG_USERNAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner2.setAdapter(new ArrayAdapter<String>(MainActivity_d2.this, android.R.layout.simple_spinner_dropdown_item, students2));
}
}
每当我点击我的textview,然后点击分享我在我的Android设备中得到该错误。这是为什么?最新解决方案是什么?我所做的就是为textview调用textisselectable属性。此视图位于listview
中07-19 09:36:41.882: E/AndroidRuntime(18380): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.app.ContextImpl.startActivity(ContextImpl.java:672)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.app.ContextImpl.startActivity(ContextImpl.java:659)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.widget.TextView.shareSelectedText(TextView.java:9493)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.widget.TextView.onTextContextMenuItem(TextView.java:9211)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.widget.Editor$TextActionModeCallback.onActionItemClicked(Editor.java:3249)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.policy.PhoneWindow$DecorView$ActionModeCallback2Wrapper.onActionItemClicked(PhoneWindow.java:3540)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.FloatingActionMode$3.onMenuItemSelected(FloatingActionMode.java:85)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.view.FloatingActionMode$4.onMenuItemClick(FloatingActionMode.java:111)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.widget.FloatingToolbar$FloatingToolbarMainPanel$1.onClick(FloatingToolbar.java:1015)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.view.View.performClick(View.java:5204)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.view.View$PerformClick.run(View.java:21153)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.os.Handler.handleCallback(Handler.java:739)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.os.Handler.dispatchMessage(Handler.java:95)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.os.Looper.loop(Looper.java:148)
07-19 09:36:41.882: E/AndroidRuntime(18380): at android.app.ActivityThread.main(ActivityThread.java:5417)
07-19 09:36:41.882: E/AndroidRuntime(18380): at java.lang.reflect.Method.invoke(Native Method)
07-19 09:36:41.882: E/AndroidRuntime(18380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
这是我的<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/itemAccountdetails"
style="@style/listViewPrimaryDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:paddingLeft="10dp"
android:layout_gravity="left"
android:gravity="left"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:singleLine="true" />
<TextView
android:id="@+id/itemAccountdetailsValue"
style="@style/listViewSecondaryDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:layout_gravity="right"
android:textIsSelectable="true"
android:gravity="right"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>
<View
android:id="@+id/item_separator"
android:layout_width="match_parent"
android:layout_height="@dimen/lvDividerHeight"
android:background="@color/lvDividerColor" />
</LinearLayout>
代码:
Activity