没有创建TextView

时间:2015-11-01 20:04:20

标签: android xml android-layout csv textview

MainActivity.java

public class MainActivity extends AppCompatActivity {
double MOA;
TextView turretClicks;
boolean noMOA;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    turretClicks = (TextView) findViewById(R.id.turretClicks);



    // Create an anonymous implementation of OnClickListener
    View.OnClickListener btnClickCalc = new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            double clicks = (MOA * 4);
            String toText = Double.toString(clicks);
            turretClicks.setText(toText);



            EditText range = (EditText) findViewById(R.id.rangeEntry);
            String stringRange = range.getText().toString();
            int finalRange = Integer.parseInt(stringRange);
            if (finalRange <= 200) {
                MOA = 0;
            }
            if (finalRange > 200 && finalRange <= 225) {
                MOA = .5;
            }
            if (finalRange > 225 && finalRange <= 250) {
                MOA = 1;
            }
            if (finalRange > 250 && finalRange <= 275) {
                MOA = 1.65;
            }
            if (finalRange > 275 && finalRange <= 300) {
                MOA = 2.25;
            }
            if (finalRange > 300 && finalRange <= 325) {
                MOA = 2.8;
            }
            if (finalRange > 325 && finalRange <= 350) {
                MOA = 3.5;
            }
            if (finalRange > 350 && finalRange <= 375) {
                MOA = 4.0;
            }
            if (finalRange > 375 && finalRange <= 400) {
                MOA = 4.75;
            }
            if (finalRange > 400 && finalRange <= 425) {
                MOA = 5.50;
            }
            if (finalRange > 425 && finalRange <= 450) {
                MOA = 6.25;
            }
            if (finalRange > 450 && finalRange <= 475) {
                MOA = 7.0;
            }
            if (finalRange > 475 && finalRange <= 500) {
                MOA = 7.5;
            }
            if (finalRange > 500 && finalRange <= 525) {
                MOA = 8.25;
            }
            if (finalRange > 525 && finalRange <= 550) {
                MOA = 9.0;
            }
            if (finalRange > 550 && finalRange <= 575) {
                MOA = 9.75;
            }
            if (finalRange > 575 && finalRange <= 600) {
                MOA = 10.5;
            }
            if (finalRange > 600 && finalRange <= 625) {
                MOA = 11.5;
            }
            if (finalRange > 625 && finalRange <= 650) {
                MOA = 12.25;
            }
            if (finalRange > 650 && finalRange <= 675) {
                MOA = 13;
            }
            if (finalRange > 675 && finalRange <= 700) {
                MOA = 14;
            }
            if (finalRange > 700) {
                noMOA = true;
            }

        }
    };


    // Capture our button from layout
    Button button = (Button) findViewById(R.id.btnClickCalc);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(btnClickCalc);



    final View.OnClickListener btnRecordRange = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            EditText range = (EditText) findViewById(R.id.rangeEntry);

            final String recordableClicks = turretClicks.toString();
            final String recordableRange = range.toString();

            try {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("recordings.csv", Context.MODE_PRIVATE));
                outputStreamWriter.write("Clicks" + " " + recordableClicks + "," + " " + "@Range" + recordableRange + "\n");
                outputStreamWriter.close();
            }
            catch (IOException e) {
                Log.e("Exception", "File write failed: " + e.toString());
            }
            Toast.makeText(MainActivity.this, "     Clicks @ Range" + "\n" + "Successfully Recorded", Toast.LENGTH_SHORT).show();
        }

    };

    // Capture our button from layout
    Button recordRange = (Button) findViewById(R.id.btnRecordRange);
    // Register the onClick listener with the implementation above
    recordRange.setOnClickListener(btnRecordRange);
    // Create an anonymous implementation of OnClickListener



    final View.OnClickListener btnToRecorded = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,RangeRecords.class));

        }

    };

    // Capture our button from layout
    Button showRecords = (Button) findViewById(R.id.btnToRecorded);
    // Register the onClick listener with the implementation above
    showRecords.setOnClickListener(btnToRecorded);
    // Create an anonymous implementation of OnClickListener

}

}

RangeRecords.java

public class RangeRecords extends ListActivity {
public static TextView ListItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_recorded_ranges);
    readFromFile("recordings.csv");
}


    public String readFromFile(String fname) {
    List<String> rangeList = new ArrayList();
    String ret = "";

    try {
        InputStream inputStream = openFileInput(fname);

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString;
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }

            rangeList.add(stringBuilder.toString());
            View linearLayout = findViewById(R.id.linLay);
            for (int i = 0; i < rangeList.size(); i++) {
                TextView value = new TextView(this);
                value.setText(i);
                value.setId(i);
                value.setTextSize(20);
                value.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ((LinearLayout) linearLayout).addView(value);
            }
        }

        inputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return ret;
}

}

content_recorded_ranges.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/linLay">

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

当我在手机上运行此程序时,它在第一项活动中完美运行。当我单击按钮切换到第二个活动时,该活动应该在TextViews中显示我记录的数据,屏幕只是白色。我可以看到顶部的Android栏,我看到我的服务和电池寿命等等,但它甚至有点模糊,好像有什么东西放在它的顶部。

我拥有ListView的唯一原因是因为编译器要求我在代码中使用它。我不想/需要它,但显然编译器认为我这样做。

我在这里做错了什么?我想在第二个活动上显示记录的数据...所以,我们计算炮塔的点击次数,射击步枪,如果这是一个很好的镜头,将数据记录到文件中,那么我们所要做的就是来回到第二个活动,查看我们以前在不同范围内拍摄的数据。

任何?一直都在这一天。

谢谢!

修改

这是现在的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"       
android:layout_width="match_parent"
android:layout_height="match_parent"   
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_recorded_ranges"
tools:context="lol2dubs.stevemoa.recorded_ranges"
android:background="#fefefe">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/linLay">
</LinearLayout>
</RelativeLayout>

RangeRecords.java

public class RangeRecords extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_recorded_ranges);
    readFromFile("recordings.csv");
}


    public String readFromFile(String fname) {
    List<String> rangeList = new ArrayList();
    String ret = "";

    try {
        InputStream inputStream = openFileInput(fname);

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString;
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);
            }

            rangeList.add(stringBuilder.toString());
            View linearLayout = findViewById(R.id.linLay);
            for (int i = 0; i < rangeList.size(); i++) {
                TextView value = new TextView(this);
                value.setText(i);
                value.setId(i);
                value.setTextSize(20);
                value.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ((LinearLayout) linearLayout).addView(value);
            }
        }

        inputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return ret;
}

}

从XML中删除扩展ListActivity和ListView后,仍然会发生同样的事情。

1 个答案:

答案 0 :(得分:0)

由于您正在扩展ListActivity,因此需要ListView。此外,ListView阻止您的TextView,因此您无法看到它。

但是,您应该坚持使用ListView,因为您正在使用动态数字显示多个文本视图。有关教程,请参阅this

如果您不想使用它,请更改

public class RangeRecords extends ListActivity

public class RangeRecords extends AppCompatActivity

删除布局上的ListView

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/linLay">

</LinearLayout>