我试图构建一个简单的应用程序,通过以下代码从URL获取Image,但找不到findViewById(FloatingActionButton)
错误找到合适的方法,帮助将不胜感激。
大多数代码都是android studio的基本版activity
的生物传感器,但无论如何都找不到故障代码。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import static singh.rahul.com.imagedownloader.R.id.fab;
public class MainActivity extends AppCompatActivity {
ImageView imgDownloaded;
public void DownloadImage(View v){
ImageDownloaderClass task=new ImageDownloaderClass();
Bitmap myRawImage;
try{
myRawImage=task.execute("Any Image URL").get();
imgDownloaded.setImageBitmap(myRawImage);
}catch(Exception e){
e.printStackTrace();
}
}
public class ImageDownloaderClass extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... urls){
try{
URL url=new URL(urls[0]);
HttpURLConnection connections=(HttpURLConnection) url.openConnection();
connections.connect();
InputStream is=connections.getInputStream();
Bitmap theBitmap= BitmapFactory.decodeStream(is);
return theBitmap;
}catch (MalformedURLException mue){
mue.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imgDownloaded=(ImageView) findViewById(R.id.imageView);
FloatingActionButton fab = (FloatingActionButton) findViewById(fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Downloading Image...", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
DownloadImage(View);
}
});
}
@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);
}
}
期待您的帮助!
答案 0 :(得分:2)
而不是fab
FloatingActionButton fab = (FloatingActionButton) findViewById(fab);
您需要传递id(来自xml布局):
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.your_fab);
另外,在fab按钮监听器中,这一行:
DownloadImage(View);
应该是:
DownloadImage(view); //lower case
答案 1 :(得分:1)
findViewById(int id)
此方法将参数作为int(视图的id)。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.yourId);
答案 2 :(得分:0)
使用此
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
而不是这个
FloatingActionButton fab = (FloatingActionButton) findViewById(fab);
希望这会有所帮助。