Java 应用程序在点击按钮时崩溃,表示试图调用虚拟方法';void android.widget.Button.setOnClickListener';
当我点击Java 应用程序在点击按钮时崩溃,表示试图调用虚拟方法';void android.widget.Button.setOnClickListener';,java,android,xml,Java,Android,Xml,当我点击activity\u home.xml的Upload Photo按钮时,此代码显示错误。在我没有添加上传活动之前,这个应用程序一直运行良好。上传活动就是将图片推送到ftp服务器。上传活动作为一个独立的apk可以正常工作。这个错误显示了当我加入这两个单独的APK并创建了一个意图b/w这两个单独的视图时 我不确定应用程序是否会在单击按钮时崩溃 日志异常 Process: com.muchmore.www.chasquido, PID: 9571 java.lang.RuntimeEx
activity\u home.xml
的Upload Photo
按钮时,此代码显示错误。在我没有添加上传活动之前,这个应用程序一直运行良好。上传活动就是将图片推送到ftp服务器。上传活动作为一个独立的apk可以正常工作。这个错误显示了当我加入这两个单独的APK并创建了一个意图b/w这两个单独的视图时
我不确定应用程序是否会在单击按钮时崩溃
日志异常
Process: com.muchmore.www.chasquido, PID: 9571
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.muchmore.www.chasquido/com.muchmore.www.chasquido.Upload}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5424)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.muchmore.www.chasquido.Upload.onCreate(Upload.java:30)
at android.app.Activity.performCreate(Activity.java:6093)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5424)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Home.java
package com.muchmore.www.chasquido;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.ImageView;
import android.content.pm.PackageInfo;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.widget.ImageButton;
import android.view.View;
// new imports
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
public class Home extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
Button btnShowLocation;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Media Player
Runnable backgroundSound = new Runnable() {
@Override
public void run() {
MediaPlayer mediaPlayer1= MediaPlayer.create(Home.this, R.raw.welcome_message);
mediaPlayer1.start();
MediaPlayer mediaPlayer2= MediaPlayer.create(Home.this, R.raw.welcome_tone);
mediaPlayer2.start();
}
};
Thread media = new Thread(backgroundSound);
media.start();
// Media Player
// Font
final TextView userMessage = (TextView) findViewById(R.id.welcome_tag);
Typeface customWelcomeMessage = (Typeface)Typeface.createFromAsset(getAssets(), "PoiretOne-Regular.ttf");
userMessage.setTypeface(customWelcomeMessage);
// Font
// Intent Import and show top Text
Bundle achieve = getIntent().getExtras();
Boolean anonymousUser = false;
if(achieve == null){
anonymousUser = true;
return;
}
String userName= achieve.getString("Username");
if(anonymousUser == false) {
userMessage.setText("Welcome " + userName + ", You can now take a snap, edit it and upload it to the Server.");
}
if(anonymousUser == true){
userMessage.setText("Welcome Annoymous User, You can't do anything here. Buzz off!!");
}
// Intent Import and show top Text
// GPS
Runnable r = new Runnable() {
@Override
public void run() {
btnShowLocation = (Button)findViewById(R.id.show_location);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPSTracker(Home.this);
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(
getApplicationContext(),
"Your Location is -\nLat: " + latitude + "\nLong: "
+ longitude, Toast.LENGTH_LONG).show();
} else {
gps.showSettingsAlert();
}
}
});
}
};
Thread th = new Thread(r);
th.start();
// GPS
// CAMERA BUTTON IMPLEMENTATION
ImageButton cameraButton = (ImageButton)findViewById(R.id.imageButton);
imageView = (ImageView)findViewById(R.id.imageView);
/* Disable the button if the user doesn't have camera */
if(!hasCamera())
cameraButton.setEnabled(false);
// CAMERA BUTTON IMPLEMENTATION
}
// Upload View Load
public void loadUpload(View view){
Log.d("MyMessage", "On Upload Button Click");
Intent intent = new Intent(Home.this, Upload.class);
startActivity(intent);
Log.d("MyMessage", "Starting Intent");
}
//
// Check if the user has a camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
// Launching the camera
public void launchCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Take a picture and pass results along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
// If you want to return the image taken
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
//Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
}
}
package com.muchmore.www.chasquido;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Upload extends Activity {
private static final int MY_INTENT_CLICK=302;
private TextView txta;
private Button btn_selectImage;
String myGlobalImagePath = "";
Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txta = (TextView) findViewById(R.id.textView1);
btn_selectImage = (Button) findViewById(R.id.btn_selectImage);
btn_selectImage.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),MY_INTENT_CLICK);
}
});
r = new Runnable() {
@Override
public void run() {
Ftp obj = new Ftp();
obj.ftpConnect("cp.mdurtk.in", "cp.mdurtk.in|cp", "cp@123", 21);
//obj.ftpUpload(myGlobalImagePath,"file.jpg", "/vikas");
obj.ftpMyUpload(myGlobalImagePath);
}
};
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == MY_INTENT_CLICK)
{
if (null == data) return;
String selectedImagePath;
Uri selectedImageUri = data.getData();
//MEDIA GALLERY
selectedImagePath = ImageFilePath.getPath(getApplicationContext(), selectedImageUri);
myGlobalImagePath = selectedImagePath;
Log.i("Image File Path", ""+selectedImagePath);
txta.setText("File Path : \n"+selectedImagePath); }
}
Thread t = new Thread(r);
t.start();
}
}
活动\u home.xml
RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.muchmore.www.chasquido.Home"
android:background="@drawable/home_background"
android:id="@+id/home_activity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome User!!!"
android:id="@+id/welcome_tag"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000"
/>
<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageButton"
android:background="@drawable/camera_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="launchCamera"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:minHeight="300dp"
android:minWidth="300dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:id="@+id/show_location"
android:layout_above="@+id/imageView"
android:layout_toRightOf="@+id/imageButton"
android:layout_toEndOf="@+id/imageButton" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload Photo"
android:id="@+id/upload"
android:layout_above="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:onClick="loadUpload" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Howdy Buddy! Select an image from your device and the image will be automatically uploaded to the server. You need not to do anything. Isn't that cool? Well yaa cool app for cool buddies."
android:textSize="14sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_selectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Select Image" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
活动上传.xml
RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.muchmore.www.chasquido.Home"
android:background="@drawable/home_background"
android:id="@+id/home_activity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome User!!!"
android:id="@+id/welcome_tag"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000"
/>
<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageButton"
android:background="@drawable/camera_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="launchCamera"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:minHeight="300dp"
android:minWidth="300dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:id="@+id/show_location"
android:layout_above="@+id/imageView"
android:layout_toRightOf="@+id/imageButton"
android:layout_toEndOf="@+id/imageButton" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload Photo"
android:id="@+id/upload"
android:layout_above="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"
android:onClick="loadUpload" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Howdy Buddy! Select an image from your device and the image will be automatically uploaded to the server. You need not to do anything. Isn't that cool? Well yaa cool app for cool buddies."
android:textSize="14sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_selectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Select Image" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
您正试图调用上载.java
,第30行上的null
对象上的方法。您正试图调用上载.java
上的null
对象上的方法,行30
您正试图调用Upload.java
上的null
对象上的方法。行30您正试图调用Upload.java
上的null
对象上的方法,行30
您将layout.xml
设置为上载活动的内容View
你应该这样做
setContentView(R.layout.activity_upload);
而不是做
setContentView(R.layout.activity_main);
您将layout.xml
设置为上载活动的内容View
时出错
你应该这样做
setContentView(R.layout.activity_upload);
而不是做
setContentView(R.layout.activity_main);
您将layout.xml
设置为上载活动的内容View
时出错
你应该这样做
setContentView(R.layout.activity_upload);
而不是做
setContentView(R.layout.activity_main);
您将layout.xml
设置为上载活动的内容View
时出错
你应该这样做
setContentView(R.layout.activity_upload);
而不是做
setContentView(R.layout.activity_main);
我没有在主页活动中看到已初始化的上载按钮。我正在看我手机上的密码。我可能找不到它。我没有在“主页”活动中看到初始化的上载按钮。我正在看我手机上的密码。我可能找不到它。我没有在“主页”活动中看到初始化的上载按钮。我正在看我手机上的密码。我可能找不到它。我没有在“主页”活动中看到初始化的上载按钮。我正在看我手机上的密码。我可能错过了。