i have added onClick in my mainactivity for my textview and i had no errors but when i run the app it crashes saying app has stopped working even though i have no errors in my c
You have wrongly initialized your TextView
As you have added TextView
in your layout and you are trying to initialize it with LinearLayout
which is wrong.
Change the LinearLayout
with TextView
in your onCreate() as below :
someLayout = (TextView) findViewById(R.id.state2);
Why are you inflating your layout two times in your code ? I do not understand why are you doing so. But i am providing the code with some relevant code try with my code.
public class MainActivity extends Activity {
private LayoutInflater inflater;
private TextView someLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
someLayout = (TextView) findViewById(R.id.state2); //layout present in activity_main
// inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
runNextTask();
}
public void runNextTask(){
// LinearLayout mInflatedLayout = (LinearLayout) inflater.inflate(R.layout.activity_main, null);
@SuppressWarnings("unused")
final TrackerInfo newInfo = new TrackerInfo();
//set up for model selection
// TextView modelTextview = (TextView)mInflatedLayout.findViewById(R.id.state2);
//someLayout.addView(mInflatedLayout);
someLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
FYI You can not add any layout in TextView
as its View
not a Layout
and in your code i have seen that you are trying to add Layout
into View
which is wrong.
Change to
public void runNextTask(View v){ // method signature is worng
Also if textView is in activity_main.xml. There is no need to inflate the layout
setContentView(R.layout.activity_main); // is enough
And initialize textView in onCreate
TextView modelTextview = (TextView)findViewById(R.id.state2);
If you want to have click listener progrmatically remove
android:onClick="runNextTask"
the problem is here someLayout = (LinearLayout) findViewById(R.id.state2);
you are trying to cast textview to linearlayout.
use yourtextview = (TextView) findViewById(R.id.state2);
Your id for linear layout and text-view both are same or what, if it is so, then change the id of linear layout. by below line of code it is clear that you have assigned same id to linear layout and text-view.
someLayout = (LinearLayout) findViewById(R.id.state2);