在onCreate方法中的textview中设置文本

时间:2014-04-17 09:54:34

标签: android android-intent textview android-tablelayout oncreate

这是我尝试解决这个问题的第三天。我有" Wyniki"活动由" Ekran"活动。在Wyniki" onCreate" metod我指定设置标签的文本,这是以Ekran的计算方式取得的:

我的Ekran.java:

package com.example.fizyka;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class Ekran extends ActionBarActivity {
    public final static String DANE_X = "Fizyka.DANE_WEJSCIE";
    public final static String EPSILON = "Fizyka.EPSILON";
    public final static String EPSILON_POTEGI = "Fizyka.EPSILON_POTEGI";
    public final static String SREDNIA = "Fizyka.SREDNIA";
    public final static String NIEPEWNOSC = "Fizyka.NIEPEWNOSC";
    public final static String EPSILON_POTEGI_SUMA = "Fizyka.EPSILON_POTEGI_SUMA";

public void srednia (View view){
     EditText wejscie = (EditText) findViewById(R.id.wejscie);
     String dane_wejscie = wejscie.getText().toString();
     String arr[] = dane_wejscie.split("\\r?\\n");
     List<Double> dane = new ArrayList<Double>();
     for(int i = 0; i < arr.length;i++){
         dane.add(Double.parseDouble(arr[i].trim().replaceAll(",", ".")));}

     //Licze srednia
     Double srednia = 0.0;
     for(int i = 0; i < dane.size();i++){
         srednia = srednia + dane.get(i);}
     srednia = srednia / arr.length;

     //Odejmuje średnią od danych
     Double[] roznice = new Double[arr.length];
     for(int i = 0;i < dane.size();i++){
         roznice[i]=dane.get(i)-srednia;
     }

     //Kwadraty roznic i ich suma
     Double[] roznice_sq = new Double[arr.length];
     Double suma_roznice_sq =0.0;
     for(int i = 0;i < dane.size();i++){
         roznice_sq[i]=Math.pow(roznice[i],2);
     }
     for(int i = 0;i < dane.size();i++){
         suma_roznice_sq=suma_roznice_sq+roznice_sq[i];
     }

     //Niepewność typu A
     double Ua=Math.sqrt((suma_roznice_sq/(arr.length*(arr.length-2))));
     //Przygotowanie do wysyłki
    Intent wysylka = new Intent(this,Wyniki.class);
    wysylka.putExtra(DANE_X, dane_wejscie);
    wysylka.putExtra(EPSILON, Arrays.toString(roznice));
    wysylka.putExtra(EPSILON_POTEGI, Arrays.toString(roznice_sq));
    wysylka.putExtra(SREDNIA, srednia);
    wysylka.putExtra(EPSILON_POTEGI_SUMA, suma_roznice_sq);
      wysylka.putExtra(NIEPEWNOSC, Ua);
      startActivity(wysylka);
}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ekran);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.ekran, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_ekran,
                    container, false);
            return rootView;
        }
    }

}

我的Wyniki.java:

package com.example.fizyka;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Wyniki extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wyniki);
        Intent odbior = getIntent();
        Double Ua_wynik = odbior.getDoubleExtra(Ekran.NIEPEWNOSC,0);
        String Ua_ww = Double.toString(Ua_wynik);
        System.out.println(Ua_ww);
        TextView textView = (TextView) findViewById(R.id.wyniki_finalne);
        textView.setText(Ua_ww);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.wyniki, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_wyniki,
                    container, false);
            return rootView;
        }
    }

}

fagment_ekran.xml:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <EditText
        android:id="@+id/wejscie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Dane..."
        android:gravity="top">
    </EditText>
    <Button
        android:id="@+id/przycisk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Oblicz"
        android:onClick="srednia"
        android:gravity="center"></Button>
    <TextView
        android:id="@+id/wynik"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</TextView>

</LinearLayout>
</ScrollView>

fragment_wyniki.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*" >


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:gravity="center"
            android:text="x"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/TextView23"
            android:gravity="center"
            android:text="ε=x-x̄"
            android:textStyle="bold">
        </TextView>

        <TextView
            android:id="@+id/TextView24"
            android:gravity="center"
            android:text="ε²"
            android:textStyle="bold">
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/wartosci_x"
            android:gravity="center"
            android:text="x"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/wartosci_epsilon"
            android:gravity="center"
            android:text="ε=x-x̄"
            android:textStyle="bold">
        </TextView>

        <TextView
            android:id="@+id/wartosci_epsilon_pow"
            android:gravity="center"
            android:text="ε²"
            android:textStyle="bold">
        </TextView>
    </TableRow>
    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

       <TextView
            android:id="@+id/wyniki_finalne"
            android:gravity="center"
            android:text="Wróć"
            android:textStyle="bold">
        </TextView>

        <TextView/>
        <Button
            android:layout_width="wrap_content"
            android:textSize="6pt"
            android:text="Wróć"
            android:gravity="center"
            />  

    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
       <TextView/>
        <TextView/>
        <Button
            android:layout_width="wrap_content"
            android:textSize="6pt"
            android:text="Eksportuj do TXT..."
            android:gravity="center"
            />  

    </TableRow>


</TableLayout>

这给我带来了一堆错误:

04-17 10:18:49.766: E/AndroidRuntime(743): FATAL EXCEPTION: main
04-17 10:18:49.766: E/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fizyka/com.example.fizyka.Wyniki}: java.lang.NullPointerException
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.os.Looper.loop(Looper.java:137)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 10:18:49.766: E/AndroidRuntime(743):  at java.lang.reflect.Method.invokeNative(Native Method)
04-17 10:18:49.766: E/AndroidRuntime(743):  at java.lang.reflect.Method.invoke(Method.java:511)
04-17 10:18:49.766: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 10:18:49.766: E/AndroidRuntime(743):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 10:18:49.766: E/AndroidRuntime(743):  at dalvik.system.NativeStart.main(Native Method)
04-17 10:18:49.766: E/AndroidRuntime(743): Caused by: java.lang.NullPointerException
04-17 10:18:49.766: E/AndroidRuntime(743):  at com.example.fizyka.Wyniki.onCreate(Wyniki.java:25)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.Activity.performCreate(Activity.java:5008)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-17 10:18:49.766: E/AndroidRuntime(743):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-17 10:18:49.766: E/AndroidRuntime(743):  ... 11 more

Wyniki.java:25是textView.setText(Ua_ww);行。我可以责怪TableLayout吗?

更新Wyniki.java:

package com.example.fizyka;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Wyniki extends ActionBarActivity {
    private static String Ua_ww;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent odbior = getIntent();
        Double Ua_wynik = odbior.getDoubleExtra(Ekran.NIEPEWNOSC,0);
        Ua_ww = Double.toString(Ua_wynik);
        System.out.println(Ua_ww);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_wyniki,null);
             TextView textView = (TextView) rootView.findViewById(R.id.wyniki_finalne);
         textView.setText(Ua_ww);

            return rootView;
        }
    }

}

3 个答案:

答案 0 :(得分:0)

第31行是什么?

在您的片段事务中,您想要用id&#34; container&#34;替换视图。我无法在您的布局中看到容器。这个NP可以从这个片段事务中抛出,因为它无法找到容器。

答案 1 :(得分:0)

Wykiny活动中,您致电 setContentView(R.layout.activity_wyniki); 这意味着以后你打电话给findViewbyId TextView textView = (TextView) findViewById(R.id.wyniki_finalne);TextView wyniki_finalne必须存在于activity_wyniki布局中。

解决方案:

添加<TextView android:id="@+id/wyniki_finalne" android:gravity="center" android:text="Wróć" android:textStyle="bold"> </TextView>

到您的布局activity_wyniki

答案 2 :(得分:0)

setContentView(R.layout.activity_wyniki);

无法在Fragments尝试如下

private String Ua_ww;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent odbior = getIntent();
    Double Ua_wynik = odbior.getDoubleExtra(Ekran.NIEPEWNOSC,0);
    Ua_ww = Double.toString(Ua_wynik);
    System.out.println(Ua_ww);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_wyniki,
                false);
         TextView textView = (TextView) rootView.findViewById(R.id.wyniki_finalne);
         textView.setText(Ua_ww);

        return rootView;
    }
}

↳ android.app.Activity ↳ android.support.v4.app.FragmentActivity ↳ android.support.v7.app.ActionBarActivity