2016-05-06 6 views
0

Ich bin ein bisschen Newb auf Android-Entwicklung. Ich versuche, eine einfache Aktivität anzuzeigen, die vollständig in Java ohne XML geschrieben ist, aber es stürzt ab. Was mache ich falsch? Hier ist der Code:Android-Aktivität in Java mit XML stürzt ab

public class TestActivity extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedinstancestate) 
    { 
     super.onCreate(savedinstancestate); 
     RelativeLayout thislayout = new RelativeLayout(this); 
     RelativeLayout.LayoutParams TextViewParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
     TextViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     TextViewParams.addRule(RelativeLayout.CENTER_VERTICAL); 
     TextView outputtext = new TextView(this); 
     outputtext.setText("Poop!"); 
     thislayout.addView(outputtext, TextViewParams); 
     setContentView(thislayout); 
    } 
} 
+1

Können Sie Ihr Fehlerprotokoll hinzufügen? –

+0

Ich bekomme nur den Fehler "Leider hat TestJavaLayout gestoppt." Ich kann das nicht mit einem Emulator testen, weil ich einen AMD-Prozessor habe, der die Emulation in Android Studio – user6191667

+0

nicht unterstützt. Suche Logcat in deinem Studio. Es wird dir zeigen, wo du es falsch machst. –

Antwort

1

Okey Sie LayoutParams initialisiert wird, aber nicht auf TextView Einstellung. Und Sie müssen auch LayoutParams für das übergeordnete Layout definieren, d. H. RelativeLayout.

Ich habe Ihren Code getestet und einige Änderungen vorgenommen. try tun dies

@Override 
public void onCreate(Bundle savedinstancestate) { 
    super.onCreate(savedinstancestate); 

    // setting RelativeLayout Params 
    RelativeLayout thislayout = new RelativeLayout(this); 
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT); 
    thislayout.setLayoutParams(params); 

    // setting TextView Params 
    TextView outputtext = new TextView(this); 
    RelativeLayout.LayoutParams TextViewParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    TextViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    TextViewParams.addRule(RelativeLayout.CENTER_VERTICAL); 
    outputtext.setLayoutParams(TextViewParams); 
    outputtext.setText("Poop!"); 

    thislayout.addView(outputtext); 
    setContentView(thislayout); 
} 

Hoffe, dass es Ihnen helfen wird.

0

Sie können Ihren Code wie folgt ändern:

public class Main2Activity extends AppCompatActivity { 



    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // setContentView(R.layout.activity_main2); 
      RelativeLayout relativeLayout=new RelativeLayout(this); 
      TextView programmedTextView=new TextView(this); 
      programmedTextView.setText("Setting my textView programmatically"); 
      programmedTextView.setTextSize(20); 
      relativeLayout.addView(programmedTextView); 
      this.setContentView(relativeLayout,new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     } 
    } 
0

korrekt sein Thema zu schreiben, meinen Sie offenbar "Android-Aktivität in Java mit NO XML abstürzt".

Zweitens scheint, dass Sie vergessen haben, Ihre Aktivität in Manifest-Datei zu beschreiben, wie:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="io.github.d2edev.myapplication"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".main.TestActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

Zumindest für mich gearbeitet. Einen schönen Tag noch!