Is it possible to call by OnClick a new Activity TextView?

3

Hello, I'm trying to call a new activity through OnClick in a TextView , but running the application crashes.

Here is the activity from which I am trying to pass:

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 product_name item is in a separate layout file that is as 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>

    
asked by anonymous 18.10.2018 / 16:27

2 answers

1

What you need to note is the following:

1st findViewById() within the activity is directly connected to its respective layout, in this case:

setContentView(R.layout.activity_sd);

3º Always post the error message so that it will be easier to help you.

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

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);
    
22.10.2018 / 18:21
4

Firstly, if this is not the solution, paste the LOG so that we can analyze it.

Make sure the name of your activity in AndroidManifest is consistent with your class, I noticed that you are declaring an activity in the manifest, and when you try to start the new activity pass a reference with a capital letter.

<activity android:name=".ProdutoActivity"></activity>
    
19.10.2018 / 00:54