2016-06-17 40 views
-3

Picture of the ErrorTextView的打印假

我是新來的Android和Java,所以請多多包涵,但這個我的代碼和權當我打開我的應用程序TextView回報false(甚至在我輸入任何東西進入我的EditText框) 。

任何建議將是有幫助的,因爲我在網上找不到任何東西。

的Java

package me.finalproject.com.apchemchemolyapp; 

import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.TextView; 


public class stoich_fragment extends Fragment 
{ 
    View rootview; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.stoich_layout, container, false); 
    return view; 

} 
public void getReactants() 
{ 
    EditText r = (EditText) getView().findViewById(R.id.reactants); 
    String reactant = r.toString(); 
    int i = 0; 
    int j = 0; 
    int k = 0; 
    int l = 0; 
    int[] arr = new int[j]; 
    String[] elements = new String[k]; 
    boolean isRunning = true; 
    String s = ""; 
    while(isRunning == true) 
    { 
     String element = ""; 
     String let = reactant.substring(i, i+1); 
     if(let.compareTo(let.toLowerCase()) > 0) 
     { 
      element += let; 
      i++; 
     } 
     else if(let.compareTo(let.toLowerCase()) == 0) 
     { 
      element += let; 
      i++; 
     } 
     else if (let.equals("0")||let.equals("1")||let.equals("2")||let.equals("3")||let.equals("4")||let.equals("5")||let.equals("6")||let.equals("7")||let.equals("8")||let.equals("9")) 
     { 
      int temp = Integer.parseInt(let); 
      arr[l] = temp; 
      elements[l] = element; 
      l++; 
      element = ""; 
     } 
     else if(i == reactant.length()-1) 
     { 
      isRunning = false; 
     } 
    } 
    // displays the elements isolated on the reactant side 
    // to test to make sure my logic works 
    for(int a = 0; a<reactant.length(); a++) 
    { 
     s += elements[a] + " : " + arr[a] + "\n"; 
    } 
    TextView beq = (TextView) getActivity().findViewById(R.id.balanced_equation); 
    beq.setText(s); 
    //String s declared in other portion of code 
} 

}

XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent"> 
<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/reactants" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="95dp" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/products" 
    android:layout_alignBottom="@+id/reactants" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/beq" 
    android:id="@+id/balanced_equation" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

+0

你需要更加描述你正在做什麼,以及它是如何失敗的。 「TextView返回false」實際上沒有多少意義。 –

+0

你的意思是空「的TextView返回false」?我最好的猜測是你沒有得到「getview()」的預期觀點。 –

+0

對不起,我添加了一個圖片鏈接,顯示我的意思是 – vdopp

回答

0

有很多你的代碼的許多問題。首先,你在你的TextView設置文本的ID:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/beq" // Delete this line 
    android:id="@+id/balanced_equation" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

如果你沒有設置你的XML文本,直到你將它設置爲TextView不會顯示任何內容。

其次,您正在使用EditText.toString()而不是EditText.getText().toString()。第一種方法將返回類實例的字符串表示形式,而不是輸入到EditText中的文本的值。

接着,要創建兩個空數組:

int j = 0; 
int k = 0; 

int[] arr = new int[j]; 
String[] elements = new String[k]; 

每個線將創建長度爲零的數組,這是不能用Java進行修改。因此,如果您的代碼曾經到達elements[1] = element這樣的行,那麼您的代碼會崩潰,並顯示ArrayIndexOutOfBoundsException。但它永遠不會到達那裏,因爲你有一個無限循環。

在任何情況下,我都會建議在處理Android開發之前查看Java基礎知識。 Oracle網站有一些很好的tutorials

+0

的東西哦等待大聲笑sry我想我可能已經上傳了一個意外的舊版本的代碼,我沒有改變bc我在那個時候對邏輯有不同的看法,但是我通過使用ArrayList修復了這個問題。代碼在EditText中拋出NullPointerException r =(EditText)getView()。findViewById(R.id.reactants); – vdopp

+0

感謝您使用TextView提供的其他XML幫助 – vdopp

相關問題