+ 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
8 Antworten
+ 8
Inside the onCreate(), you should write anything after super.onCreate() and setContentView() lines.
+ 6
It it still didn't run
+ 6
Thanks for all your contributions bros My.problem.resolved=true
+ 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);
}
}
+ 5
A little later after posting this question I tried that too
+ 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 '}'
}
+ 1
Your change method is inside the onCreate method.
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.