2017-08-26 42 views
-1
this is what the android monitor is showing: 

08-26 19:04:17.456 10081-10089 /? E/art:無法將回復發送給調試器:損壞的管道08-26 19:04:17.456 10081-10089 /? I/art:調試器不再處於活動狀態08-26 19:04:17.582 10081-10081 /? I/InstantRun:啓動即時運行服務器:主要是 進程08-26 19:04:17.628 10081-10081 /? W/art:在Android 4.1之前, 方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode ) 將不正確地覆蓋 中的包私有方法android.graphics.drawable.Drawable 08-26 19:04:17.736 10081-10081 /? D/AndroidRuntime:關閉VM 08-26 19:04:17.736 10081-10081 /? E/AndroidRuntime:致命異常:主
過程:com.example.sayandeep.notepad,PID:10081
了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.example.sayandeep.notepad/com。示例.sayandeep.notepad.MainActivity}: 顯示java.lang.NullPointerException:嘗試在一個空 對象引用
調用虛擬方法 '無效 android.widget.EditText.setText(java.lang.CharSequence中)' 在 機器人。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)
在android.os.Handler.dispatchMessage(處理程序。的java:102)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5254)
在java.lang.reflect.Method.invoke (本機方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
引起:java.lang.NullPointerException:試圖調用虛擬 方法'void android.widget.EditText.setText(java.lang .CharSequence)」 上的空對象引用
在 com.example.sayandeep.notepad.MainActivity.onCreate(MainActivity.java:38) 在android.app.Activity.performCreate(Activity.java:5990)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java: 1303) at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread。的java:5254)
在java.lang.reflect.Method.invoke(本機方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在 com.android.internal.os .ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)08-26 19:04:26.858 10081-10081/com.example。 sayandeep.notepad I/Process: 發送信號。 PID:10081 SIG:9崩潰記事本應用程序時,空白Acticity

主要活動

package com.example.sayandeep.notepad; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 

import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.File; 

import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

public class MainActivity extends AppCompatActivity { 
EditText content; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     content=(EditText)findViewById(R.id.content); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Save("Note1.txt"); 
      } 
     }); 
     content.setText((Open("Note1.txt"))); 

    } 
    public void Save(String filename) { 
     try { 
      OutputStreamWriter out = new OutputStreamWriter(openFileOutput(filename, 0)); 
      out.write(content.getText().toString()); 
      out.close(); 
      Toast.makeText(this, "Note saved!", Toast.LENGTH_SHORT).show(); 
     } catch (Throwable t) { 
      Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
    public boolean FileExists(String filename) { 
     File file=getBaseContext().getFileStreamPath(filename); 
     return file.exists(); 
    } 
    public String Open(String filename) { 
     String temp=""; 
     if(FileExists(filename)) 
     { 
      try 
      { 
       InputStream in=openFileInput(filename); 
       if(in!=null) 
       { 
        InputStreamReader tmp=new InputStreamReader(in); 
        BufferedReader reader=new BufferedReader(tmp); 
        String str; 
        StringBuilder buf=new StringBuilder(); 
        while((str=reader.readLine())!=null) 
        {buf.append(str+"\n");}in.close(); 
        temp=buf.toString(); 

       } 

      }catch(java.io.FileNotFoundException e){}catch(Throwable t){ 
       Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();} 

    }return temp; 
    } 
    @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); 
    } 

} 

#Activity_main.xml# 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.sayandeep.notepad.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_menu_save" 
     android:onClick="onClick"/> 
</android.support.design.widget.CoordinatorLayout> 

#Content_main.xml#

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
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="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.sayandeep.notepad.MainActivity" 
tools:showIn="@layout/activity_main"> 
<EditText android:id="@+id/content" 
android:layout_width="391dp" 
android:layout_height="523dp" 
android:hint="Type here..." 
android:gravity="top" 
android:inputType="textPersonName" 
android:layout_centerVertical="true" 
android:layout_alignParentStart="true" /> 

</android.support.constraint.ConstraintLayout> 

回答

0

你應該包括你的主佈局content_main.xml。在浮動操作按鈕之前將其添加到主佈局。閱讀here

<include layout="@layout/content_main"/> 
+0

請問您可以將您鏈接的網頁的一些內容移到這裏。鏈接可能會加班,所以最好放在這裏,其他人都可以看到。 – SteveFest