Question

I have followed the tutorial to the letter on the android developers website on how to launch a new activity. However there are 2 errors that I cant clear, and they are in code taken directly from the tutorial?

public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class); //error 1
                EditText editText = (EditText) findViewById(R.id.edit_message); //error 2
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

 }

error 1 is suggesting remove arguments to match intent() error 2 is saying - Cannot make a static reference to the non-static method findViewById(int) from the type Activity

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

   <Button
   android:id="@id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/button_send" />

     </LinearLayout>
Was it helpful?

Solution

Move this out of Fragment. The below must be in Activity.

 public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class); //error 1
            EditText editText = (EditText) findViewById(R.id.edit_message); //error 2
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

I guess you have

android:onClick="sendMessage"

for button in xml

I also guess the view belongs to the fragment layout. If the button and EditText belongs to the fragment_main.xml then

  EditText editText;
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    editText = (EditText)rootView.findViewById(R.id.edit_message) 
    Button b = (Button) rootView.findViewById(R.id.button1); //button id
    b.setOnClickListener( new OnClickListener()
    {
       @Override
       public void onClick(View V) 
       {
            Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); /
            String message = editText.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
       } 
    });  
    return rootView;
}

And you can get rid of android:onClick="sendMessage"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top