mercredi 8 avril 2015

How to properly use an Activity with a fragment within?


Vote count:

0




I'm trying to get an Activity which will work with a fragment (I wil work with more fragments later). I created both a class and a layout for such fragment, and included them at Activity's layout.


Classes


This is what my Activity class looks like:



public class Expressa extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


This is what Fragment's class looks like:



public class MessageFragment extends Fragment {

EditText messageField;
private onNewMessageListener listener;

public View OnCreateVie(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.message_fragment_layout,container,false);

messageField = (EditText) view.findViewById(R.id.messageField);

return view;
}

public void doTextToSpeech(View view) {

String text = messageField.getText().toString().trim();
new TextToSpeechServices().execute(text);
}

public interface onNewMessageListener {

public void newMessage(String msg);

}

}


Layouts


Activity layout:



LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >

<fragment
android:id="@+id/messageFragment"
class="apps.android.expressa.expressa.MessageFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/message_fragment_layout" />
</LinearLayout>


Fragment's:


However I'm always getting an IllegalStateException complaining MessageFragment didn't create a view.


This is fragment's layout and this is complete stacktrace What have I got wrong?



asked 1 min ago







How to properly use an Activity with a fragment within?

Aucun commentaire:

Enregistrer un commentaire