Aarti Wale Aarti Wale - 5 months ago 36
Android Question

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

I tried clean project and even rebuild project, there is no change .... The application has stop, and the log error is :
enter image description here

package com.example.dota.jsonphp;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends Activity implements View.OnClickListener
{
private EditText textview1;
private Button btnhit;

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

textview1 = (EditText) findViewById(R.id.textview);
btnhit = (Button) findViewById(R.id.btnhit);
btnhit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
new jsontask().execute("http://localhost/phppage.php");
}


public class jsontask extends AsyncTask<String, String, String> {


@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));

StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);

}
return buffer.toString();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
textview1.setText(result);
}

}


}


Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">

<TextView
android:id="@+id/textview"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:text="helloworld"
/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/btnhit"
android:text="HIt"
android:layout_weight="0.06" />

</LinearLayout>

Answer
private TextView textview1;
textview1 = (TextView) findViewById(R.id.textview);
Comments