+ 5

Have i done something wrong?

I have made an app on Android but it doesnt run whenever I try to run it an error log appears saying an error occurred

26th Mar 2017, 10:08 AM
Prabhakar Dev
Prabhakar Dev - avatar
8 odpowiedzi
+ 8
Inside the onCreate(), you should write anything after super.onCreate() and setContentView() lines.
26th Mar 2017, 11:37 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 6
It it still didn't run
26th Mar 2017, 11:36 AM
Prabhakar Dev
Prabhakar Dev - avatar
+ 6
Thanks for all your contributions bros My.problem.resolved=true
26th Mar 2017, 4:15 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 5
import android.widget.*; import android.app.*; import android.os.*; import android.view.*; import android.view.View.*; public class MainActivity extends Activity{ TextView t=(TextView)findViewById(R.id.mainTextView); Button b=(Button)findViewById(R.id.mainButton); View.OnClickListener e=new View.OnClickListener() { public void onClick(View v) { t.setText("Hi bro!"); } }; @Override protected void onCreate(Bundle savedInstanceState) { b.setOnClickListener(e); super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
26th Mar 2017, 10:08 AM
Prabhakar Dev
Prabhakar Dev - avatar
+ 5
A little later after posting this question I tried that too
26th Mar 2017, 3:35 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 5
Ok resolved the onclick error but there are some other errors coming and I don't think they should come main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:text="Yo!" android:id="@+id/mainTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/mainButton" android:text="Change" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="change" /> </LinearLayout> Main activity.java: package com.mycompany.myapp2l; import android.app.*; import android.os.*; import android.widget.*; import android.view.View; /* *@author:Prabhakar */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView t=(TextView)findViewById(R.id.mainTextView); Button b=(Button)findViewById(R.id.mainButton);//Here it says missing '}' public void change(View v){ t.setText("Hello");//Here it says unknown entity 't' } }//Here it says unexpected '}' }
26th Mar 2017, 3:58 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 1
Your change method is inside the onCreate method.
26th Mar 2017, 4:15 PM
ChaoticDawg
ChaoticDawg - avatar
0
Try removing your last import statement. You're importing View two different ways. Each of which changes how you access OnClickListener. IE View.OnClickListener vs OnClickListener.
26th Mar 2017, 11:05 AM
ChaoticDawg
ChaoticDawg - avatar