Textview geht nicht...

  • 13 Antworten
  • Letztes Antwortdatum
T

tomycat

Ambitioniertes Mitglied
0
hallo,
im Textview steht nur hello w. aber nicht Please wait ?!?

Code:
public class normal extends Activity implements OnClickListener{
    private Button zurueck;
    private Button jetztsenden;
    TextView resultArea; // tut
    @Override
    public void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
     
    resultArea= (TextView) findViewById(R.id.resultArea);
     
    resultArea = new TextView(this);  //tut
    resultArea.setText("Please wait.");  //tut
    setContentView(resultArea);  //tut
    new FetchSQL().execute();  //tut
     
     
    setContentView(R.layout.normal);
     
    zurueck = (Button) findViewById(R.id._zurueck);
    zurueck.setOnClickListener(this);
     
    jetztsenden = (Button) findViewById(R.id.jetzt_senden);
    jetztsenden.setOnClickListener(this);
     
    //////////// Servereinstellungen laden...
    //////// Serveradresse:
/// kommt später rein ....
    ///////////////

    }

     
    public void onClick(View v)
    {
        if (v == zurueck)
    {
     
       Intent intent = new Intent(this, MainActivity.class);
         this.startActivity(intent);
    }
        if (v == jetztsenden)
    {
          /// postgressql
         /// postgressql ende
        Toast.makeText(getBaseContext(),
  "senden war efolgreich!",
  Toast.LENGTH_LONG).show();
     
    }
       
       
       
  }   
    //AB hier tut
    private class FetchSQL extends AsyncTask<Void,Void,String> {
    @Override
    protected String doInBackground(Void... params) {
    String retval = "";
    try {
    Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    retval = e.toString();
    }
    String url = "jdbc:postgresql://10.0.2.2/dbname?user=username&password=pass";
    Connection conn;
    try {
    DriverManager.setLoginTimeout(5);
    conn = DriverManager.getConnection(url);
    Statement st = conn.createStatement();
    String sql;
    sql = "SELECT 1";
    ResultSet rs = st.executeQuery(sql);
    while(rs.next()) {
    retval = rs.getString(1);
    }
    rs.close();
    st.close();
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    retval = e.toString();
    }
    return retval;
    }
    @Override
    protected void onPostExecute(String value) {
    resultArea.setText(value);
    }
      // Bis hier tut
     
    }}
die Layout:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
   
   
   



  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >

<TextView  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:text="@string/server_name_"
  />

<EditText
  android:id="@+id/server_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />

 <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >

<TextView  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:text="@string/server_pass_"
  />

<EditText
  android:id="@+id/server_pass"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />

  <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
  android:id="@+id/resultArea"
  android:layout_width="100dp"
  android:layout_height="20dp"
  android:text="hello_world"
  />
  <Button
  android:id="@+id/jetzt_senden"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/jetzt_senden"
  android:layout_weight="2"
  />
  <Button
  android:id="@+id/_zurueck"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/_zurueck"
  android:layout_weight="1"
  />
  </LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
 
Moin,

Die Zeile löschen:
Code:
resultArea = new TextView(this);  //tut

Dann müsste es funktionieren. Weil du in Zeile dem TextView schon sagt das es ein TextView ist und auf das UI zugreifen soll.
Code:
resultArea= (TextView) findViewById(R.id.resultArea);

Mit der Zeile
Code:
resultArea = new TextView(this);  //tut
würdest du dann das zuvor erstellte TextView überschreiben.
 
Außerdem nur weil du ein View Objekt erstellst heißt es nicht dass es zu sehen ist, da es weder inflatet noch zu einem Container/Layout hinzugefügt wurde.
 
wenn ich diese Zeile lösche stützt mein Prog ab.
Code:
02-03 18:31:18.773: D/libEGL(9836): loaded /system/lib/egl/libEGL_VIVANTE.so
02-03 18:31:18.899: D/libEGL(9836): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
02-03 18:31:18.903: D/libEGL(9836): loaded /system/lib/egl/libGLESv2_VIVANTE.so
02-03 18:31:18.939: D/OpenGLRenderer(9836): Enabling debug mode 0
02-03 18:31:20.566: D/AndroidRuntime(9836): Shutting down VM
02-03 18:31:20.566: W/dalvikvm(9836): threadid=1: thread exiting with uncaught exception (group=0x40da8930)
02-03 18:31:20.573: E/AndroidRuntime(9836): FATAL EXCEPTION: main
02-03 18:31:20.573: E/AndroidRuntime(9836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kinup/com.example.kinup.normal}: java.lang.NullPointerException
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread.access$600(ActivityThread.java:145)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.os.Looper.loop(Looper.java:137)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread.main(ActivityThread.java:5106)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at dalvik.system.NativeStart.main(Native Method)
02-03 18:31:20.573: E/AndroidRuntime(9836): Caused by: java.lang.NullPointerException
02-03 18:31:20.573: E/AndroidRuntime(9836):    at com.example.kinup.normal.onCreate(normal.java:54)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.Activity.performCreate(Activity.java:5109)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-03 18:31:20.573: E/AndroidRuntime(9836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-03 18:31:20.573: E/AndroidRuntime(9836):    ... 11 more
 
Bitte nochmal die Zeile 54 aus der normal.java posten.
 
Code:
  @Override
    public void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
     
    resultArea= (TextView) findViewById(R.id.resultArea);
     
    //  resultArea = new TextView(this);  //tut
    resultArea.setText("Please wait.");  //tut   <---------------------------Zeile 54
    setContentView(resultArea);  //tut
    new FetchSQL().execute();  //tut
     
     
    setContentView(R.layout.normal);
 
Hallo,

bevor du in deinen Code einem View mit findViewById() an den RootView bindest, muss der RootView definiert werden.
Das heißt, du darfst erst nach setContenView(R.layout.normal) die Methode findViewById() ausführen. Sonst suchst du eine ID, für die kein View vorhanden ist. Die Methode kann keine "Pointer"(eigentlich eine Referenz) zum Objekt herstellen, das nicht existiert. Die Folge ist ein NullPointerException..

PS: Du solltest nur einmal den RootView erzeugen (Denn es kann nur eine Wurzel geben ;) ).
 
Zuletzt bearbeitet:
Hier die Lösung. Wie Jaiel schon sagte muss du zuerst setContentView(R.layout.normal); ausführen und dann das TextView bearbeiten. Aber du solltest dir nochmal die Grundlagen anschauen.

Code:
  @Override
    public void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.normal);

    resultArea= (TextView) findViewById(R.id.resultArea);

    resultArea.setText("Please wait.");

markus.tullius war schneller :-D
 
ok,
verstaden, klingt logisch. thx
stürtzt immernoch ab.
Code:
public class normal extends Activity implements OnClickListener{
    private Button zurueck;
    private Button jetztsenden;
    TextView resultArea; // tut
    @Override
    public void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.normal);
    resultArea= (TextView) findViewById(R.id.resultArea);
     
    //  resultArea = new TextView(this);  //tut
    resultArea.setText("Please wait.");  //tut
    setContentView(resultArea);  //tut
    new FetchSQL().execute();  //tut
 
    zurueck = (Button) findViewById(R.id._zurueck);
    zurueck.setOnClickListener(this);
     
    jetztsenden = (Button) findViewById(R.id.jetzt_senden);
    jetztsenden.setOnClickListener(this);
     
    //////////// Servereinstellungen laden...

Mein Layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:text="@string/server_name_"
  />
<EditText
  android:id="@+id/server_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
 <LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:text="@string/server_pass_"
  />
<EditText
  android:id="@+id/server_pass"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
  <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <TextView
  android:id="@+id/resultArea"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"   
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="96dp"
  android:text="@string/jetzt_senden" />
  />
  <Button
  android:id="@+id/jetzt_senden"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/jetzt_senden"
  android:layout_weight="2"
  />
  <Button
  android:id="@+id/_zurueck"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/_zurueck"
  android:layout_weight="1"
  />
  </LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
[doublepost=1454527653,1454527548][/doublepost]die Fehlermeldung:
Code:
02-03 20:22:27.574: D/libEGL(21523): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
02-03 20:22:27.581: D/libEGL(21523): loaded /system/lib/egl/libGLESv2_VIVANTE.so
02-03 20:22:27.617: D/OpenGLRenderer(21523): Enabling debug mode 0
02-03 20:22:41.588: D/AndroidRuntime(21523): Shutting down VM
02-03 20:22:41.588: W/dalvikvm(21523): threadid=1: thread exiting with uncaught exception (group=0x40da8930)
02-03 20:22:41.594: E/AndroidRuntime(21523): FATAL EXCEPTION: main
02-03 20:22:41.594: E/AndroidRuntime(21523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kinup/com.example.kinup.normal}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread.access$600(ActivityThread.java:145)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.os.Looper.loop(Looper.java:137)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread.main(ActivityThread.java:5106)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at dalvik.system.NativeStart.main(Native Method)
02-03 20:22:41.594: E/AndroidRuntime(21523): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3348)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.view.ViewGroup.addView(ViewGroup.java:3219)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.view.ViewGroup.addView(ViewGroup.java:3195)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:307)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:295)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.Activity.setContentView(Activity.java:1904)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at com.example.kinup.normal.onCreate(normal.java:55)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.Activity.performCreate(Activity.java:5109)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-03 20:22:41.594: E/AndroidRuntime(21523):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
02-03 20:22:41.594: E/AndroidRuntime(21523):    ... 11 more
02-03 20:22:43.001: I/Process(21523): Sending signal. PID: 21523 SIG: 9
 
Ganz einfach, in onCreate darf nur einmal setContenView ausgeführt werden. Der Befehl erzeugt das Wurzelverzeichnis für alle Views.
Wenn es erzeugt wurde, kannst du es nur erzetzen, wenn du alle Views entfernst. Beim zweiten Aufruf von setContenView() kann kein neues Wurzelverzeichnis erzeugt werden, da schon eines existiert.


setContentView(resultArea); löst den Fehler aus.
 
Zuletzt bearbeitet:
stimmt stimmt stimmt thx,
hmmmmmmm ich hoffe ich nerve nicht, ich will nur den Code in mein Projekt einfügen.
Tutorial: Accessing a PostgreSQL database from an Android Device
ich muss folgedes jetzt ausblenden, dass der Code ausgefügt wird.
Code:
  @Override
    protected void onPostExecute(String value) {
    ///  resultArea.setText(value); // <------------------------hier
    }
      // Bis hier tut

can not be resolved
 
Schicke mal die Fehlermeldung. Für was brauchst du einen direkten Zugriff auf eine Datenbank von einem Android-Device?

Wie schon der Autor anmerkt ist es ein gutes Beispiel für einen Zugriff auf eine PostgreSQL Datenbank. Für ein produktiven System sollte man den Code nicht nehmen. Ich hoffe, das soll nur in einem lokalen Netz funktionieren.

PS: Nach Deinen Problemen zu urteilen, solltest Du dich mehr mit den Grundlagen von Android beschäftigen, bevor du weitermacht. Wenn man fremden Code einbindet, sollte man die grundlegende Funktionsweise schon verstanden habe.
 
123thomas schrieb:
Hier die Lösung. Wie Jaiel schon sagte muss du zuerst setContentView(R.layout.normal);
Indirekt habe ich das mitunter gemeint, aber die Credits gehen an markus.tullius
 
thx all,
@Markus Tullis, du hast recht ich sollte den code erstmal verstehen, und das habe ich jetzt gemacht.Jetzt habe ich ein sql Problem.
 
Zurück
Oben Unten