2017-12-24 1 views
-3

내 응용 프로그램을 시작한 후 잠시 후. 처음에는 앱이 문제없이 실행되었지만 메인 화면 전에 로그인 화면을 추가 한 후에는 오류가 발생하여 중지됩니다. "안타깝게도 AutoparesOnline이 중지되었습니다. 안드로이드를 처음 접했고 어떤 도움을 주셔서 감사합니다. 로그는 activity.xml 및 자바 코드이다. 이는 XML이다; y 응용 프로그램 실행 후 잠시 후 멈춤

.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:theme="@style/newTheme" 
    android:layout_weight="6" 
    android:background="@android:color/black"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="7dp" 
     android:layout_marginStart="7dp"> 

     <TextView 
      android:text="@string/welcome" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/tv_welcome" 
      android:textStyle="normal|bold" 
      android:textColor="@color/btn_login_bg" 
      android:textSize="24sp" 
      android:textAlignment="center" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1" 
     android:background="@drawable/circle" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <ImageView 
      android:contentDescription="A mechanic with his tools" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/handy_man" 
      android:id="@+id/imageView9" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginTop="19dp" 
      tools:ignore="HardcodedText" /> 

    </RelativeLayout> 

    <Button 
     android:text="@string/Continue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnStartAnotherActivity" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

이것은 자바 코드 여기

import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.ProgressBar; 


    public class MainActivity extends AppCompatActivity{ 

    Button btn1; 
    private ProgressBar pBar; 

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

     btn1 = (Button) findViewById(R.id.btnStartMainActivity); 
     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this, AnotherActivity.class); 
       startActivity(intent); 
      } 
     }); 
     pBar.setVisibility(View.VISIBLE); 



    } 

에러의 일부이다.

위 0
FATAL EXCEPTION: main 
                    Process: co.autosparesonline.ke, PID: 3011 
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{co.autosparesonline.ke/co.autosparesonline.ke.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:154) 
                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
                     at co.autosparesonline.ke.MainActivity.onCreate(MainActivity.java:21) 
                     at android.app.Activity.performCreate(Activity.java:6662) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
                     at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:154)  
                     at android.app.ActivityThread.main(ActivityThread.java:6077)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

답변

-1
btn1 = (Button)btn1.findViewById(R.id.btnStartMainActivity); 

변경 :

btn1 = (Button) findViewById(R.id.btnStartMainActivity); 

은 또한 당신이 그것에 대해 죄송합니다 setVisibility

+0

를 호출하기 전에 pBar을 할당하지 않습니다. 그건 나에게 어리석은 짓이야, 나는 여전히 같은 오류가 발생하기 때문에 그것을 편집하게한다. –

+0

pBar – Niko

+0

에 대해 findViewById를 호출하지 않았습니다. 하지만 난 여전히 오류가 어떻게이 라인에서 오는 지 않습니다 "btn1 = (Button) findViewById (R.id.btnStartMainActivity);" –

관련 문제