我的Android应用程序进展顺利,除了一个似乎没有任何原因的奇怪黑盒的外观。
这是XML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
这是我的代码来处理出现黑框的情况:
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
//int i = position;
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.scroll_layout, fragment).commit();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String area = sharedPreferences.getString("area", "Home");
//String[] menuSelection = getResources().getStringArray(R.array.main_menu_array);
ArrayList<String> menuArray = getMenuArray(area);
String[] menuSelection = menuArray.toArray(new String[menuArray.size()]);
String loc = menuSelection[position];
Editor editor = sharedPreferences.edit();
editor.putString("area", loc);
editor.commit();
if (loc.equals("Area1") || loc.equals("Area2") || loc.equals("Area3") || loc.equals("Vendors") || loc.equals("Announcements")){
editor = sharedPreferences.edit();
editor.putString("section", loc);
editor.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mForumTitles[position]);
ArrayList<String> arList = getMenuArray(loc);
String[] newArr = arList.toArray(new String[arList.size()]);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, newArr));
mDrawerLayout.closeDrawer(mDrawerList);
setMainView(loc, newArr[0]);
}else{
loc = sharedPreferences.getString("section", "Speakers");
mDrawerList.setItemChecked(position, true);
//setTitle(mForumTitles[position]);
ArrayList<String> arList = getMenuArray(loc);
String[] newArr = arList.toArray(new String[arList.size()]);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, newArr));
mDrawerLayout.closeDrawer(mDrawerList);
setMainView(loc, newArr[position]);
}
}
private void setMainView(String section, String selection){
mTableView = (LinearLayout) findViewById(R.id.scroll_layout);
mTableView.removeAllViews();
ArrayList<String> info = getInfoArray(section, selection);
String[] instructions = info.get(0).split("\\|");
String[] values = info.get(1).split("\\|");
for(int i=0; i < instructions.length; i++){
if (instructions[i].equals("@image")){
//TableRow newRow = new TableRow(this);
ImageView newImage = new ImageView(this);
newImage.setBackgroundColor(Color.BLUE);
//newRow.addView(newImage);
mTableView.addView(newImage);
}else{
//TableRow newRow = new TableRow(this);
TextView newText = new TextView(this);
newText.setText(instructions[i] + ": " + values[i]);
newText.setTextColor(Color.BLACK);
newText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
//newRow.addView(newText);
mTableView.addView(newText);
}
}
//TableRow newRow = new TableRow(this);
TextView newText = new TextView(this);
//newText.setBackgroundColor(Color.BLUE);
newText.setText(info.get(2));
newText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
newText.setTextColor(Color.BLACK);
//newRow.addView(newText);
mTableView.setPadding(60, 20, 60, 0);
mTableView.addView(newText);
}
private ArrayList<String> getInfoArray(String section, String selection){
ArrayList<String> fileArray = new ArrayList<String>();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
String str = "http://www.mysite./area/" + section + "/" + selection + ".txt";
//URL url = new URL(");
str = str.replace(" ", "%20");
str = str.replace(",", "%2C");
URL url = new URL(str);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
fileArray.add(str);
}
in.close();
} catch (MalformedURLException e1) {
} catch (IOException e) {
}
return fileArray;
}
private ArrayList<String> getMenuArray(String section){
ArrayList<String> fileArray = new ArrayList<String>();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
String str = "http://www.mysite.com/area/" + section + ".txt";
//URL url = new URL(");
str = str.replace(" ", "%20");
str = str.replace(",", "%2C");
URL url = new URL(str);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
str = str + "";
while ((str = in.readLine()) != null) {
fileArray.add(str);
}
in.close();
} catch (MalformedURLException e1) {
} catch (IOException e) {
}
}
这是截图:
答案 0 :(得分:0)
问题解决了。我创建了一个新项目,复制了所有代码,问题就消失了。那好吧。感谢任何试图解决它的人。