2015-11-01 103 views
1

MainActivity.java沒有TextViews創建

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> 

當我我的手機上運行這個程序,它完美的第一項活動。當我點擊按鈕切換到第二個活動,它應該在TextView中顯示我錄製的數據時,屏幕只是白色。我可以在頂部看到安卓欄,在那裏我可以看到我的服務和電池壽命等,但它甚至有點模糊,好像有什麼東西放在頂部。

我有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控件。

回答

0

因爲您正在擴展ListActivity,因此需要ListView。另外,ListView會阻止你的TextView,所以你看不到它。

但是,您應該在使用ListView時使用Stick,因爲您使用動態數字顯示多個文本視圖。有關教程,請參閱this

如果你不想使用它,然後在你的佈局

<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> 
+0

呀改變

public class RangeRecords extends ListActivity 

public class RangeRecords extends AppCompatActivity 

刪除的ListView。我是個白癡。謝謝。 – lol2dubs

+0

清理項目解決了這個問題。所以現在我在編譯時出現這個錯誤: 錯誤:執行失敗的任務':app:dexDebug'。 > com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:進程'命令'C:\ Program Files \ Java \ jdk1.8.0_60 \ bin \ java.exe''已完成非零退出值2 – lol2dubs

+0

因此,您的解決方案有意義,但無法正常工作。我刪除了ListView並更改了活動,第二個活動仍然是純白的,Android欄仍然看起來好像被某些東西所覆蓋。將更新頂部的新代碼。 – lol2dubs