Is it possible to call Onclick from Textview a new Activity?

Asked

Viewed 565 times

3

Hello, I’m trying to call a new Activity through OnClick in a TextView, but when running the application generates a crash.

Here’s the Activity of from the one I’m trying to get through:

public class sdActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "TesteGigaservicesAPP";

//list view dos produtos
public ListView lv;

//list view adapter
ArrayAdapter<String> adapter;

//barra de busca
AutoCompleteTextView inputSearch;



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

    //produtos da busca
   String[] produtos = {
           "Produto 1",
           "Produto 2",
           "Produto 3",
           "Produto 4",
           "Produto 5"
   };

   lv = (ListView)findViewById(R.id.listaDProdutos);
   inputSearch = (AutoCompleteTextView)findViewById(R.id.barradepesquisa);

   //popula a lista
   adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, produtos);
   lv.setAdapter(adapter);

    //

   //aciona a barra de buscas

    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        sdActivity.this.adapter.getFilter().filter(cs);
        }



        @Override
        public void afterTextChanged(Editable s) {

        }
    });



    /*problemas de implementação*/

    TextView ver = (TextView)findViewById(R.id.product_name);

    ver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(sdActivity.this, ProdutoActivity.class));
            //teste para passar sob o elemento de uma view de layout
        }
    });






    //botão que volta a página anterior
    Button btVoltarUm = (Button)findViewById(R.id.btVoltarUm);

    btVoltarUm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(sdActivity.this, MainActivity.class));
        }
    });
}
}

The item product_name is in a separate layout file that is like list_item.xml:

<?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">

    <!-- Single ListItem -->

    <!-- Nome do produto listado -->


    <TextView
        android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" />

</LinearLayout>

This is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testegigaservices.testegigaservices">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".sdActivity"
        android:label="@string/pgBusca"
        android:windowSoftInputMode="stateHidden"></activity>
    <activity android:name=".produtoActivity"></activity>
</application>

  • "when running the application generates a crash" - The first step in this case is to consult the Log to see which error message and which line it was wrong on. You should take this error message and ask the question

  • ok, from the logcat queries I will insert the logs on the next try.

2 answers

4

First, if this is not the solution, paste the LOG so we can analyze.

Make sure your Activity name on Androidmanifest is consistent with your class, I’ve noticed that you’re declaring an Activity on the manifest, and when you try to start the new Activity, you’ll be uppercase.

<activity android:name=".ProdutoActivity"></activity>

1


What you need to observe is the following:

1st O findViewById() within Activity is directly linked to its respective layout, in this case:

setContentView(R.layout.activity_sd);

2º The error probably happens precisely because the findViewById() is looking for the Textview that actually is in another layout (list_item.xml), consequently he will not find:

3rd Always post the message of error, to make it easier to help you.

4th Solution to recognize the click on one of the list items:

private OnItemClickListener quandoOhItemForClicado = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
           startActivity(new Intent(sdActivity.this, ProdutoActivity.class));
    }
};

listView.setOnItemClickListener(quandoOhItemForClicado);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.